Initialize and copy the constructor directly. The value assignment operator is reloaded.

Source: Internet
Author: User

Compiling environment (vc6.0)
C++ Supports two initialization modes: Direct initialization and replication initialization.

Direct initialization directly calls the constructors that match the real parameters, and the copy constructor is always called during replication initialization.

Example of direct initialization:

String DOTS (10, 'A ');

Example of replication initialization:

(1) Use one object of the class to initialize another object

Cat cat1;

Cat cat2 (cat1 );

(2) Another example of initializing another object with an object of the class

Cat cat2 = cat1;

(3) passing objects as parameters

F (Cat A) {} (if it is referenced, it is not called)

(4) object as the return value of the Function

Cat F ()

{

Cat;

Return;

}

(If the parameter is a reference and the returned parameter is a reference, it is not called)

It can be seen that:

Direct initialization uses basic data types as parameters of the constructor.

Replication initialization uses an object as the object for copying constructors.

 

Assignment operators:

(1) initialize the variable at the same time

String demo3 = demo1 + demo2;

In this case, you need to check the return value of the addition operator to determine whether to call the copy constructor or constructor.

(2) define variables before Initialization

String demo3;

Demo3 = demo1 + demo2;

In this case, you need to check the return value of the addition operator to determine whether to call the copy constructor, constructor, or the copy operator overload.

 

In the following discussion, we will mainly focus on calling constructor, copy constructor, and value assignment operator overload!

# Include <iostream> # include <string> using namespace STD; Class string {char name [256]; public: string (char * Str) {cout <STR <"constructor" <Endl; strcpy (name, STR) ;}string (string & S) {cout <"copy constructor" <Endl; strcpy (name, S. name) ;}string () {cout <"Default structure" <Endl ;}~ String () {} string operator + (const string &); void operator = (const string & S) {strcpy (name, S. name); cout <"value assignment" <Endl ;}void display () {cout <"the string is:" <name <Endl ;}}; static char * STR; string: Operator + (const string & A) {string s; cout <"addition operation" <Endl; strcpy (S. name, name); strcat (S. name,. name); Return s;/* strcpy (STR, name); strcat (STR,. name); Return string (STR); */} void main () {STR = new char [256]; string demo1 ("virsual C ++ "); string demo2 ("6.0"); // demo1.display (); // demo2.display (); // string demo3; // demo3 = demo1 + demo2; string demo3 = demo1 + demo2; demo3.display ();/* string demo4 = demo3 + "programming"; demo4.display (); Delete STR; cout <"-----------" <Endl; string demo5; demo5 = demo4; demo5.display ();*/}

In this example, we can get different results through modifications: (we 'd better experiment, or it would be hard to understand)

We mainly discuss the running results of string demo3 = demo1 + demo2;

(1) The operator is initialized at the same time as defined. It is only possible to call constructor and copy constructor!

String demo3 = demo1 + demo2;

Because the returned value of the overloaded addition operator is an object, the copy constructor is called.

(2) If the addition operator changes

String string: Operator + (const string & A) {cout <"addition operation" <Endl;/* string s; strcpy (S. name, name); strcat (S. name,. name); Return s; */strcpy (STR, name); strcat (STR,. name); Return string (STR );}

Because the function returns a constructor, it calls the constructor instead of copying the constructor.

As you can see, if not all functions require class objects to be returned, you must call the copy constructor.

(3) define the variable before initialization.

String demo3; demo3 = demo1 + demo2;

Form of addition Operators

String string: Operator + (const string & A) {cout <"addition operation" <Endl; string s; strcpy (S. name, name); strcat (S. name,. name); Return s;/* strcpy (STR, name); strcat (STR,. name); Return string (STR );*/}

Because the location of the replication operator changes, and the addition operator overload returns a local object.

The duplicate constructor is called before the value assignment operator is called.

Form of addition Operators

String string: Operator + (const string & A) {cout <"addition operation" <Endl;/* string s; strcpy (S. name, name); strcat (S. name,. name); Return s; */strcpy (STR, name); strcat (STR,. name); Return string (STR );}

Only the constructor is called.

(4) If the copy operator is not overloaded
Form of addition Operators

String string: Operator + (const string & A) {cout <"addition operation" <Endl; string s; strcpy (S. name, name); strcat (S. name,. name); Return s;/* strcpy (STR, name); strcat (STR,. name); Return string (STR );*/}

Only the copy constructor is called.

Form of addition Operators

String string: Operator + (const string & A) {cout <"addition operation" <Endl;/* string s; strcpy (S. name, name); strcat (S. name,. name); Return s; */strcpy (STR, name); strcat (STR,. name); Return string (STR );}

Then the constructor is called.

The above is a program in the textbook, which summarizes the call problems of these functions.

Because I just want to test the call problems of various functions, some situations may not be allowed. For example, if there is a pointer in this class, there must be an overload of the value assignment operator.

 

There are two situations where copying constructor and value assignment operator overloading are usually required (copying constructor and value assignment operator overloading usually occur together)

1. member variables containing pointers in the class.

2. initialize the object.

 

Differences between copy construction and value assignment operator overloading

The replication operator does more than the copy structure because it involves discarding the original content and copying the new content.

Please refer to: http://www.vckbase.com/document/viewdoc? Id = 788

 

My understanding may be limited. Please give me more comments!

 

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.