9

I am learning C++ and was reading copy constructor from the C++: The Complete Reference. The books says that

It is permissible for a copy constructor to have additional parameters as long as they have default arguments defined for them. However, in all cases the first parameter must be a reference to the object doing the initializing.

But I am confused that how we are going to pass those additional parameters? I am sure there should be some way which is not given in the book and which I am unable to figure out. Can anyone help me out?

EDIT: Also is it possible to pass these extra parameters in all three cases i.e.

  • When one object explicitly initializes another, such as in a declaration
  • When a copy of an object is made to be passed to a function
  • When a temporary object is generated (most commonly, as a return value)
Nic
  • 6,211
  • 10
  • 46
  • 69
Ravi Gupta
  • 6,258
  • 17
  • 56
  • 79
  • 1
    _Herbert Schildt_'s books are [looked down](http://www.seebs.net/c/c_tcn4e.html) by the [community](http://stackoverflow.com/questions/18385418/c-meaning-of-a-statement-combining-typedef-and-typename#comment26999919_18385418). It's because of the amount of errors in each and every page, bad practises it professes. [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list?rq=1) should help you choose a good one. – legends2k Oct 10 '13 at 18:18

2 Answers2

13

Here is a simple example:

class A {
    //...
public:
    A (const A&, bool deep = false) {
        if (!deep) { /* make a shallow copy */ }
        else { /* make a deep copy */ }
    }
};

void foo (A x) { /*...*/ }
A bar () { /*...*/ return A(); }
A a_var;

In this example, the parameter is defaulted to false, meaning the default copy constructor will be shallow.

A b(a_var);       // b gets a shallow copy of a
foo(b);           // foo() receives a shallow copy

However, it would be possible to effect a deep copy by passing in true in the second parameter.

A b(a_var, true); // b gets a deep copy of a
foo(A(b, true));  // foo receives a shallow copy of a deep copy

Similarly, for a function returning an A, the returned copy would be shallow, since it is using the default, but the receiver can make it deep when it receives it.

A b(bar());       // shallow
A b(bar(), true); // deep

Remember when you define a copy constructor, it is likely to mean you will need to define a destructor and overload the assignment operator (the rule of three).

jxh
  • 69,070
  • 8
  • 110
  • 193
  • and what about the other 2 cases i.e passing an object to a function by value and returning object? ... i have updated the question for the same. – Ravi Gupta Jun 08 '12 at 05:37
1

Think of it this way: there is only the notion of constructor. When the compiler decides a copy needs to be made, it looks for a constructor that can be called by passing in a single object of type T. Because of this special use case, we conventionally call the constructor chosen a "copy" constructor.