Consider the following class:
class A {
char *p;
int a, b, c, d;
public:
A(const &A);
};
Note that I have to define a copy constructor in order to do a deep copy of "p". This has two issues:
Most of the fields should simply be copied. Copying them one by one is ugly and error prone.
More importantly, whenever a new attribute is added to the class, the copy constructor needs to be updated, which creates a maintenance nightmare.
I would personally like to do something like:
A(const A &a) : A(a)
{
// do deep copy of p
:::
}
So the default copy constructor is called first and then the deep copy is performed.
Unfortunately this doesn't seem to work.
Is there any better way to do this? One restriction - I can't use shared/smart pointers.
Sbi's suggestions make a lot of sense. I think I'll go with creating wrapper classes for handling the resource. I don't want to user shared_ptr since boost libraries may not be available on all platforms (at least not in standard distributions, OpenSolaris is an example).
I still think it would have been great if you could somehow make the compiler to create the default constructor/assignment operators for you and you could just add your functionality on top of it. The manually created copy constructor/assignment operator functions I think will be a hassle to create and a nightmare to maintain. So my personal rule of thumb would be to avoid custom copy constructors/assignment operators at all cost.
Thanks everybody for their responses and helpful information and sorry about typos in my question. I was typing it from my phone.