One of C ++ learning, mastering classes and objects

Source: Internet
Author: User

 

Understanding classes and objects

 

--- This article is based on the knowledge of classes and objects that you already know.

 

Object Structure:

 

The only correct method for using constructors on the stack:

ClassName myclass (5 );

Use the constructor on the stack:

ClassName * myclass = new ClassName (5 );

Declaring an object on the stack calls its constructor, and declaring an object pointer of a class on the stack does not need to call the constructor immediately.

To call the default constructor on the stack, remove the constructor parentheses.

Once you define your own constructor, the system will not automatically generate default constructor.

That is, if no constructor is explicitly defined, the system automatically generates a default constructor (0 parameter constructor)

If a copy constructor is not explicitly defined, the system automatically generates a copy constructor that is passed by value.

 

When defining constructors, use the initialization list whenever possible. Warning: data members will be initialized in the order they appear in the class definition, rather than in the initialization list.

 

If the value of a parameter is not changed in a function or class method, use const reference as much as possible to pass the parameter.

 

 

Object revocation:

If no destructor exists, the system automatically generates one.

Once an object on the stack is out of scope, the object will call the destructor to cancel the object.

Therefore, do not return references to objects defined in functions or class methods.

 

 

 

Object assignment:

As shown below:

SpreadsheetCell myCell (5), antherCell;

AntherCell = myCell;

You may want to say that myCell is "copied" to antherCell. However, in the C ++ world, "copy" only appears during object initialization. If an object already has a value, the value must be overwritten or overwritten ., More accurately, it is "value assignment ". C ++ provides the copy constructor for the copy operation. Since the copy constructor is a constructor, it can only be used for object creation, rather than assigning values to objects in the future. Therefore, C ++ provides the value assignment operator = overload for the value assignment operation. If you do not define a value assignment operator, the system automatically generates a default value assignment operator =, which is almost identical to the default copy operation. However, unlike the copy constructor, the value assignment operator returns a reference to the object because the value assignment can be chained.

MyCell = antherCell = otherCell;

 

 

Differentiate assignment and copy:

SpreadsheetCell myCell (5 );

SpreadsheetCell antherCell (myCell); // copy (copy) constructor

 

// It is also constructed by the replication constructor. operator =! Is not called !, Equivalent to SpreadsheetCell aThirdCell (myCell );

SpreadsheetCell aThirdCell = myCell;

 

AntherCell = myCell; // call operator =! This is because the anther has been constructed.

 

To sum up, = does not necessarily mean that operator = is called to assign values. When it is used on the same row declared by the variable, it is short for the copy constructor.

 

 

 

Object as return value:

SpreadsheetCell myCell (5 );

String s1;

S1 = myCell. getString ();

Resolution: When getString () returns the mstringl of the object myCell, the compiler calls a string copy constructor to create an anonymous string object. When this result is assigned to s1, the value assignment operator is called for s1 using this temporary string as the parameter. Then the temporary string object will be revoked.

For:

SpreadsheetCell myCell (5 );

String s1 = myCell. getString ();

Resolution: When getString () returns the mstringl of the object myCell, the compiler calls a string copy constructor to create an anonymous string object. Then s1 calls the copy constructor instead of the value assignment operator.

 

 

 

Copy constructors and object members:

If an object member contains other objects, the copy constructor generated by the compiler recursively calls the copy constructor of each object. If you write your own copy constructor, you can use the initialization list to provide the same semantics. If a data member is not included in the initialization list, the compiler initializes it by default (that is, the default constructor that calls the 0 parameter of the object) before executing the code in the constructor body. Therefore, once the initialization list is used, when the constructor body is executed, all object members have been initialized.

You can also write the copy constructor without initializing the list, as shown in the following figure: www.2cto.com

SpreadsheetCell: SpreadsheetCel (const SpreadsheetCel & src)

{

MValue = src. mValue;

MString = src. mString;

}

When a value is assigned to a data member in the copy constructor, the value assignment operator is called instead of the copy constructor of the data member.

From my dream pursued by me

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.