C + + Primer Plus reading notes--12th class and dynamic memory allocation

Source: Internet
Author: User
Tags shallow copy

Chapter 12th class and dynamic memory allocation

1. Static data members are declared in the class declaration and initialized in the file containing the class method. Initializes the class that the static member belongs to by using the scope operator. However, if the static member is an integer or enum-type const, it can be initialized in the class declaration.

Declaration and initialization of static members of the p426-p427 class

Strnbad.h

Class Stringbad

{

Private

static int num_strings;

...

};

Strnbad.cpp

int stringbad::num_strings = 0;

Static member variables cannot be initialized in a class declaration because the declaration describes how to allocate memory, but does not allocate memory. Note that the initialization statement indicates the type and uses the scope operator, but does not use the keyword static. Initialization is done in the method file, not in the class declaration file, and if the header file is included in several other files in the head file, multiple initialization copies appear, causing an error.

2. The C + + class automatically provides the following member functions:

1) default constructor, if no constructor is defined;

2) Default destructor, if not defined;

3) Copy constructor, if not defined;

4) Assignment operator, if not defined;

5) The address operator, if not defined.

C++11 provides two additional special member functions, the move constructor and the move assignment operator.

3. A constructor with parameters can also be a default constructor, as long as all parameters are default values.

4. The copy constructor is used to copy an object into the newly created object. That is, it is used to initialize the process (including passing parameters by value), rather than during regular assignment. The prototype of a class's copy constructor is usually as follows:

Class_name (const class_name &);

It takes a constant reference to a class object as a parameter.

5. When the copy constructor is called

When you create a new object and initialize it to a homogeneous existing object, the copy constructor is called, including when the function passes the object by value or returns the object. (a function that passes an object by reference saves the time it takes to call the constructor and the space to store the new object)

6. The default copy constructor replicates non-static members (member replication, also known as shallow copy) on a per-member basis, copying the values of the members. Static members are not affected because they belong to the entire class, not to individual objects.

7. If a class contains a pointer member initialized with new, you should define a copy constructor to copy the data pointed to, not the pointer, which is called deep copy. Shallow copy (member copy) just duplicates the pointer value. Copying only lightly copies the pointer information, rather than digging deep to copy the structure of the pointer reference.

8. Assignment operators

When an existing object is assigned to another existing object, an overloaded assignment operator is used.

The assignment operator is not necessarily used when initializing an object. For example:

Stringbad MeToo = knot;

Here, MeToo is a newly created object that is initialized to the value of knot, so the copy constructor is used. However, the implementation may also be handled in two steps: Create a temporary object using the copy constructor, and then copy the value of the temporary object to the new object by assigning a value.

9. The implementation of the assignment operator function is similar to the copy constructor, but there are some differences.

1) Since the target object may reference the previously allocated data, the function should use delete[] to release the data.

2) The function should avoid assigning the object to itself, otherwise releasing the memory operation may delete the object's contents before the object is re-assigned.

3) The function returns a reference to the calling object.

Stringbad & stringbad::operator= (const Stringbad & ST)

{

if (this = = &st)

return *this;

delete [] str;

len = St.len;

str = new Char [len + 1];

strcpy (str, ST.STR);

return *this;

}

Null is a C-language macro that represents a null pointer. C++11 introduces the new keyword nullptr, which is used to represent a null pointer.

One. p439-p440 provides two brackets to denote the reason for the function to access characters

12. Static class member functions

member functions can be declared static, and function declarations must contain the keyword static, which cannot contain the keyword static if the function definition is independent. There are two important consequences of this:

1) First, static member functions cannot be called through objects, and static member functions cannot use the this pointer. If a static member function is declared in the public part, you can invoke it using the class name and the scope resolution operator.

2) because static member functions are not associated with a particular object, only static data members can be used.

p449--p451.

For a description of the returned object

If a method or function returns a local object, it should return a local object instead of a reference to the object. In this case, the copy constructor is used to generate the returned object. If a method or function returns an object of a class that does not have a public copy constructor, such as the Ostream class, it must return a reference to such an object. Finally, some methods or functions, such as overloaded assignment operators, can return objects, or they can return references to objects, in which case the reference should be preferred because it is more efficient.

14. If the object was created with new, its destructor is only called when you explicitly delete an object using Delete.

p456-p459 explicitly invokes a destructor for an object created using the locate new operator.

Ostream & operator<< (ostream & OS, const C_name & obj)

{

Os << ...;

return OS;

}

C_name is the class name. If the class provides a public way to return the content you want, you can use this method in an operator function so that you do not have to set them as friends.

17. Nested Structures and classes

A struct, class, or enumeration declared in a class declaration is referred to as being nested within a class and scoped to the entire class. This declaration does not create a data object, but simply specifies the type that can be used in the class. If the declaration is made in the private part of the class, the declared type can be used only in this class, and if the declaration is in the public part, the declared type may be used from the outside of the class through the scope resolution operator.

18. List of member initialization

Conceptually, when a constructor is called, the object is created before the code in parentheses is executed (the member variable is allocated memory), and then the program enters the parentheses and stores the value in memory using regular assignment methods. For const class members and class members that are declared as references, you must use the initialization list in the constructor to initialize them at creation time. For other simple data members, there is no difference between using the initialization list and assigning values in the function body. It is more efficient to use a member initialization list for a member that is itself a class object because it uses the copy constructor directly, reducing the step of calling the assignment operator function again.

Grammar:

Classy::classy (int n, int m): Mem1 (n), mem2 (0), MEM3 (n * m + 2)

{

...

}

In summary, please note the following points:

1) This format can only be used for constructors;

2) The non-static const data member must be initialized in this format;

3) The reference data member must be initialized in this format.

Data members are initialized in the same order as they appear in the class declaration, regardless of the order in which they are arranged in the initializer.

In-class initialization of c++11.

C++11 allows you to initialize in a more intuitive way:

Class classy

{

int mem1 = 10;

Cons tint mem2 = 20;

...

}

This is equivalent to using the member initialization list in the constructor.

If the constructor for the member initialization list is used at the same time, the list overrides these default initial values.

20. Pseudo-Private method:

If the class does not currently need to provide a copy constructor and assignment operator, but may need to replicate the object in the future, you may forget that you have not provided the appropriate code for replication, in which case the program compiles and runs, but the result is really confusing and even crashes. Pseudo-private methods can be used at this time.

Class Queue

{

Private

Queue (const queue & q) {}

Queue & operator= (const queue & q) {return *this} ...

}

There are two functions: first, avoid the default method definitions that would have been automatically generated. Second, because these methods are private, they cannot be used extensively.

Queue Nick (NIP); Not allowed

Tuck = Nip; Not allowed

Therefore, rather than facing unforeseen operational failures in the future, it is better to get an easy-to-follow compilation error that states that these methods are inaccessible. In addition, this method is useful when defining classes whose objects do not allow replication.

C + + Primer Plus reading notes--12th class and dynamic memory allocation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.