(c + +) about copy constructor copy Constructor

Source: Internet
Author: User

Topic:

In which of the following scenarios is a Copy Constructor called or invoked?

A. When no conversion function exists for converting the class object to another class object

B. When a existing object is assigned a object of its own class

C    . when a function receives as an argument, an object of the class, by value

D    . when a function returns an object of the class by value

E    . when creating a object and initializing it with a object of its own class

Ideas:

Answer: CDE

The copy constructor is called in the following cases:

Class a{};

1, the Class object in the form of a value as a function parameter, such as void Func (a a);

2, the function return value is the value of the class object, such as A func ();

3. Create a class and initialize it, such as Class B; Class A (B);

Extended:

1. What is a copy constructor?

For a class X, if the first parameter of a constructor is one of the following:
A) x&
b) Const x&
c) Volatile x&
d) Const volatile x&
And no other parameters or other parameters have default values, then this function is a copy constructor.

X::X (const x&);  // is a copy constructor    x::x (Xint=1//

2. Can there be more than one copy constructor in a class?

More than one copy constructor can exist in a class

class x {      public:        x (const x&);        X (x&);            // OK    };  

Note that if there is only one copy constructor in a class with a parameter of x&, you cannot use a const x or volatile X object to implement copy initialization.

class x {   public:     x ();     X (x&);   };    Const X CX;    = CX;    // Error

If a copy constructor is not defined in a class, the compiler automatically produces a default copy constructor.
This default parameter may be x::x (const x&) or x::x (x&), which is chosen by the compiler depending on the context.

The default copy constructor behaves as follows:
The default copy constructor executes the same order as other user-defined constructors, executing the construction of the subclass after the Father class.
The copy constructor performs the action of a member copy (memberwise copy) on each data member in the class.
A) If the data member is an instance of a class, call the copy constructor of this class.
b) If the data member is an array, perform a bitwise copy of each of the pairs.
c) If the data member is a number, such as int,double, then the built-in assignment operator of the system is called to assign the value.

3. Can a copy constructor be generated by a member function template?

Copy constructors cannot be generated by member function templates.

struct X {       template<typename t>       const t&);    // Not copy ctor, T can ' t is X          template<typename t>       operator Const t&);  // Not copy the ' t, T can ' t be X    };     

The reason is simple, the member function template does not change the language rules, and the language rules say that if the program needs a copy constructor and you do not declare it, then the compiler will automatically generate one for you. Therefore, the member function template does not prevent the compiler from generating copy constructors.

Assignment operator overloading also follows the same rules. (see effective C + + 3edition, ITEM45)

(c + +) about copy constructor copy Constructor

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.