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
See also  How to Avoid Money Transfer Scams  

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist