views:

245

answers:

4

$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.

+7  A: 

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);
};
Evan Teran
+1 because that looks like an interesting read.
James McNellis
@Evan Teran: Vow!. Never imagined that creative use of the additional parameter to copy constructor!. Yet to read is completely though...
Chubsdad
@Chubs, @James: Note that this implementation is exclusive to allowing different allocator types. Although the STL *should* have an equivalent copy-contents, assign-allocator constructor, it would be implemented as another function, not a default argument.
Potatoswatter
+9  A: 

The old std::basic_string does have one too:

basic_string(const basic_string& s, 
         size_type pos = 0, size_type n = npos)
dirkgently
@dirkgently: cool!. I did not know about this. Also intellisense in VS 2010 does not show this upint main(){ string s1 = "SELL"; string s2(s1, 1, 3); s2 = "T" + s2;}
Chubsdad
+1  A: 

Very cool. Looks like a good way to pass hints to the copy constructor. Some example situations I can think of where this might be useful:

  • Copying a data structure where the copy will then be populated with additional data

    The additional parameter would hint at the likely soon -to-be-needed capacity to optimize allocation. The default capacity would be a sentinel value to indicate just use the default capacity or the capacity of the original data structure.

  • Whether to do a deep copy or shallow copy of member values

    The default might be to do a deep copy for safety, but advanced uses could take advantage of specifying rare occasions of when its safe to do a shallow copy.

  • Verbatim copy vs logical copy

    Hint whether the data structure's copy constructor should copy the original data structure's internal structure verbatim or whether its safe to optimize or consolidate it. For example, the hint could indicate a tree should be balanced as it is copied.

Bert F
A: 

I have a situation where I require the copy constructor to have more than one parameter in one of my class involving deep copy.

Basically the problem is that an object contains another object that need to keep track of it through a pointer, but a normal deep copy would only copy the pointer value and not the correct memory location of the new object.

By disabling the normal copy constructor and using the variant with two parameters I'm able to deep copy correctly my object.

[Edit]: looking through my code it seems that it is even more common than I though as I use it at a couple of place elsewhere too.

Here is a code sample for the curious (this is a simplified version and is actually a little more complicated)

//----------------------------------------------------------------------------- 
scan_point::scan_point(scan_point const& rhs, simulation* sim_)
: m(rhs.m), sim(sim_)
//----------------------------------------------------------------------------- 
{    

}

--

simulation_(simulation_ const& rhs)
{
    //...
    for(typename ContainerType::const_iterator it = rhs.spContainer->begin(), endIt = rhs.spContainer->end();
        it != endIt; it++)
    {
        spContainer->push_back(new scan_point(*it, this));
    }
}

--

To makes things less painful during copy I use smart_ptr class that allow deep copy and (in this case specifically) embed my members in a struct to makes the compiler auto-generate the copying for about all other members (see for a try to a short example: http://stackoverflow.com/questions/469696/what-is-your-most-useful-c-c-utility/1609496#1609496).

n1ck
If you are interested with the smart_ptr I know was loki smart pointer and axter (maybe other). axter had one or two small bugs but is the one I use since they offered pretty much the same functionnality for my use. Loki is probably bigger if you want to use deep copy. Both of them didn't seem to be much features added to I think.
n1ck
Not sure why it happened, but I edited a couple of spelling mistakes and the newlines magically returned. Weird.
Michael Myers
@mmyers: I think it was related to a rollback. Basically I tried to add the info about deep copy as a comment and deleted the text, then changed my mind and tried to rollback. I'm not totally sure it is the rollback though.
n1ck
@mmyers: maybe me trying to do things to fast too. I'll try to take my time next time. I wanted to update my post really fast it most certainly did not help.
n1ck