"C + + Primer Plus" Reading notes of the Ten-class and dynamic memory allocation

Source: Internet
Author: User
Tags shallow copy static class

Chapter 12th class and dynamic memory allocation

1. Static member variables cannot be initialized in a class declaration because the declaration describes how to allocate memory, but does not allocate memory. You can use separate statements for initialization outside of the class declaration, because static class members are stored separately, not part of an object. Note: Static members are declared in the class declaration and initialized in the file that contains 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 enumerated const, it can be initialized in the class declaration.

 2. When you use an object to initialize another new object, the compiler automatically generates a copy constructor because it creates a copy of the object. The prototype of the copy constructor is: ClassName (const classname &);

3. C + + automatically provides the following functions : ① default constructor, if no constructor is defined ② copy constructor, if no ③ assignment operator is defined, if no ④ default destructor is defined, if no ⑤ address operator is defined

4. 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. For example, assuming that motto is a Stringbad class object, the following 4 declarations call the copy constructor:

①stringbad ditto (motto); ②stringbad Metto=motto;③stringbad Also=stringbad (motto); ④stringbad *p=new Stringbad (motto) Where ②③ may use the copy constructor to create Metto and also directly, or you might use a copy constructor to generate a temporary object and then assign the contents of the temporary object to Metto and also, depending on the implementation.

5. The compiler will use the copy constructor whenever the program generates a copy of the object. Specifically, the copy constructor is used when a function is passed by value (creating a copy of the original variable) or when the function returns an object. The copy constructor is also used when the compiler generates a temporary object.

6. The default copy constructor replicates the non-static constructor ( shallow copy ), copying the value of the member. If the member itself is a class object, the copy constructor of the class is used to copy the member function. Static functions are not affected.

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 also known as deep replication . A shallow copy is also called a member copy, which simply duplicates the pointer value without digging deep to copy the structure of the pointer reference.

8. assignment operator : C + + allows class objects to be assigned values, which are implemented by automatically assigning values to class overloads. The prototype of this operator is as follows:

ClassName & classname::operator= (const classname &);

9, when to use the assignment operator: When an existing object is assigned to another object, an overloaded assignment operator is used. Note: initializing (creating a new object) always invokes the copy constructor, and an assignment operator may also be invoked using the = operator. The assignment operator does not create a new object.

10, similar to the copy constructor, the implicit implementation of the assignment operator also replicates the members individually (shallow copy). If the member itself is a class object, the program uses the assignment operator defined for the class to copy the member. The workaround is to provide the assignment operator (deep copy) definition for problems caused by the inappropriate assignment operator. The implementation is similar to the copy constructor, but there are some differences. An example:

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

{

if (this==&st)//Check self copy

return *this;

delete [] str; Frees the memory previously pointed to by the member pointer

Len=st.len;

Str=new char[len+1];

std::strcpy (STR,ST.STR);

return *this; Returns a reference to the calling object

}

11. Because static member functions are not associated with a particular object, only static data members can be used.

12. Overloading >> Operator:

IStream & Operator>> (IStream & is,string & ST)

{

Char temp[10]; Assuming that the number of characters entered is not more than 10

Is.get (temp,10); The value of the IStream object is set to False if for some reason (such as reaching the end of the file, or reading a blank line), causing the input to fail

if (IS)

St=temp;

while (Is&&is.get ()! = ' \ n ')//Discard extra characters

Continue

return is;

}

13, if (! CIN) detect empty line!!

14, you can use new in one constructor to initialize the pointer, and in another constructor initialize the pointer to null (NULL or 0), because the delete (whether with brackets or not) can be used for null pointers.

15. If the function returns the object passed to him, you can increase the efficiency of the method by passing the reference. The return object calls the copy constructor, and the return reference does not.

16. overloaded operators << functions, the return type must be ostream & not just ostream. If the return type Ostream, the copy constructor of the Ostream class is called, and Ostream does not have a public copy constructor.

17. Summary: If a method or function returns a local object, the object should be returned 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 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 and 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 of its high efficiency.

18. member Initialization list : classname::classname (int n,int m): Mem1 (n), Men2 (0), Men3 (n*m+2) {//...} Conceptually, these initialization work is done at object creation time, and no code in parentheses is executed at this time. For simple data members, there is no difference between using the member initialization list and using assignments in the body of the function. However, using member initialization lists is more efficient for members who are themselves class objects. Note: ① This format can only be used for constructors. ② must initialize non-static const data members in this format. ③ must initialize the reference data member in this format.

  19. nested structures and class declarations in a class: 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 that class, and if the declaration is made in the public part, the declared type may be used from the outside of the class through the scope resolution operator. For example, if node is declared in the public part of the queue class, you can declare a variable of type Queue::node outside of the class.  

"C + + Primer Plus" Reading notes of the Ten-class and dynamic memory allocation

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.