C ++ object model note copy constructor

Source: Internet
Author: User

Copy constructor
Copy constructor: initializes another object based on the content of one object. (The key lies in Initialization)
In three cases, the copy constructor is called.
Class X {......};
(1) x; X xx = x // copy the constructor instead of assigning values.
(2) void Foo (X); // object parameters
(3) foobar () {X; return X;} // return object

If the class does not provide the displayed copy constructor, the compiler uses the bit-by-bit copy function. That is to say, the object data member value is copied to another object. If an object member is encountered, it is recursive.
Class string
{
Public: // The class does not provide the displayed copy constructor.

PRIVATE:
Char * STR;
Int Len;
};

Class word
{
Public: // The class does not provide the displayed copy constructor.

PRIVATE:
Int occurs;
String STR: // The class contains object members.
};

When a bit is copied successively, the int occurs is copied sequentially to the class string after the string is encountered.

 

In the following cases, the compiler does not use the bitwise successive copy function, but instead generates the default copy constructor.
(1) When the class contains an object member, while the latter class declares a copy constructor (including compiled by the programmer and synthesized by the compiler)
Class string
{
Public:
String (const char *);
String (const string &) // class provides the displayed copy constructor
~ String ();
PRIVATE:
Char * STR;
Int Len;
};

Class word
{
Public: // The class does not provide the displayed copy constructor.

PRIVATE:
Int occurs;
String STR:
};
// It is merged into one: word: Word (const word & WD) {Str. String: string (WD. Str); occurs = WD. occurs ;}

(2) When a class inherits a base class and the latter has a copy constructor
Class word: Public String
{
Public: // The class does not provide the displayed copy constructor.

PRIVATE:
Int occurs;
String STR:
} // Is merged into one
(3) When the class declares a virtual function
Class word
{
Public: // The class does not provide the displayed copy constructor.
Extends Ural cout (); // class declares virtual functions
PRIVATE:
Int occurs;
String STR:
} // For the same reason as the constructor, copying constructor processes virtual function pointers and virtual function tables.
(4) When a class is derived from an inheritance chain that has virtual inheritance
Class zoo {...};
Class racconn: Public Virtual zoo {....} ;
Class readpanda: Public racconn {...};

At this time, it is not a class object that requires initialization of another class object, but a base class object that requires the class object to be initialized.
Raccoonn Rocky; racconn little = Rocky; // a simple bitwise successive copy is enough.
Redpanda littered; racconnn littecritter = Rocky;
// A simple bitwise successive copy won't work. The compiler must explicitly initialize the pointer inherited by littercritter.

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.