I frequently run into the problem, that I must extend a compiler generated copy constructor. Example:
class xyz;
class C
{
...
int a, b, c;
std::set<int> mySet;
xyz *some_private_ptr;
};
Assume, that some_private_ptr
should only be copied under certain conditions. For other conditions it should be reset to NULL
on copy. So I have to write a copy constructor like:
C::C(const C &other) :
a(other.a),
b(other.b),
c(other.c),
mySet(other.mySet)
{
if(CanCopy(other.some_private_ptr)) // matches condition
some_private_ptr = other.some_private_ptr;
else
some_private_ptr = NULL;
}
The problem is that the class might have a number of data members, and that I very likely may forget to update the copy constructor when I add a data member. It would be very nice if I just could write.
C::C(const C &other) :
C::default_copy(other)
{
if(CanCopy(other.some_private_ptr)) // matches condition
some_private_ptr = other.some_private_ptr;
else
some_private_ptr = NULL;
}
This would make my code more safe and easier to maintain. Unfortunately I don't know of such a possibility. Is there any?
See Question&Answers more detail:os