Copy Designer Options

In the copy constructor, why should the arguments have default values ​​associated with them? What happens if there are no default values ​​associated with them and the constructor has more than one argument?

For instance:

X(const X& copy_from_me, int = 10);

has a default value for int, but this is:

X(const X& copy_from_me, int);

no. What happens in this second case?

http://en.wikipedia.org/wiki/Copy_constructor

+5
source share
4 answers

The copy constructor always accepts one parameter, refers to the type for which it belongs, possibly other parameters, but they must have default values.

, , .

, :

T(const &T obj);

.
:

T obj1(obj2);      <--------- Direct Initialization
T obj1 = obj2;     <--------- Copy Initialization

, , , .
, , , .

+9

, , , - . , .

, :

 X(const X&, int);

- . :

 X x1;
 X x2(x1);

x2? .

, :

 X(const X&, int = 0);
+9

, .

, . , .

+3
source

The copy constructor should only be called with an object of the type to be copied:

Thing t1;     // default constructor
Thing t2(t1); // copy constructor

If there are additional arguments without default values, the constructor cannot be called like this, so it is not a copy constructor.

+1
source

All Articles