Copy Constructor’s Signatures

Copy Constructor’s Signatures

C++ defines four possible forms for a class’s copy constructor. For a class called X, a copy constructor can have one of the following forms:

 X(X&);X(const X&);X(volatile X&);X(const volatile X&);

Note that the copy constructor’s parameter cannot be passed by value. Therefore, the following forms are all illegal:

 X(X); // error, pass by valueX(const X); // dittoX(X*); // error, only references are allowed

A copy constructor can have only one parameter; additional parameters are allowed only if they have default values. For example:

 X(const X&, int n=0); // OK, 2nd param has a default valueX(const X&, int n=0, void *p=NULL); // dittoX(const X&, int n); // Illegal


Share the Post: