$12.8/2 - 'A non-template constructor for class X is a copy constructor if its first parameter is of type X&, const X&, volatile X& or const volatile X&, and either there are no other parameters or else all other parameters have default arguments (8.3.6).106)'
So far, I have not come across any example of a situation where there is a need to declare a copy constructor with additional default parameters.
Would like to know any real time use of such a copy constructor which take more than one parameter.
A copy constructor has as its first parameter a (possibly const or volatile) reference to its own class type. It can have more arguments, but the rest must have default values associated with them.
A copy constructor has one parameter that is a reference to the type that is copied. It can have additional parameters, if these have default values.
The technique of having two (or more) constructors in a class is known as constructor overloading. A class can have multiple constructors that differ in the number and/or type of their parameters. It's not, however, possible to have two constructors with the exact same parameters.
When we create our own copy constructor, we pass an object by reference and we generally pass it as a const reference. One reason for passing const reference is, we should use const in C++ wherever possible so that objects are not accidentally modified.
The old std::basic_string
does have one too:
basic_string(const basic_string& s,
size_type pos = 0, size_type n = npos)
The BDE allocator [PDF Link] used this quirk. For example their array allocator looked like this:
template <typename T>
class bde::Array {
public:
Array(bde::Allocator *allocator = 0);
Array(const Array &rhs, bde::Allocator *allocator = 0);
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With