Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an easy to maintain copy constructor

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:

  1. Most of the fields should simply be copied. Copying them one by one is ugly and error prone.

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

like image 281
Roux hass Avatar asked Jul 07 '10 07:07

Roux hass


People also ask

Does C++ automatically create a copy constructor for us as well?

Normally the compiler automatically creates a copy constructor for each class (known as an implicit copy constructor) but for special cases the programmer creates the copy constructor, known as a user-defined copy constructor.

Why do we create copy constructor?

Copy constructor is used to initialize the members of a newly created object by copying the members of an already existing object. Copy constructor takes a reference to an object of the same class as an argument.

What is copy constructor explain with example?

When Copy Constructor is called. Copy Constructor is called in the following scenarios: When we initialize the object with another existing object of the same class type. For example, Student s1 = s2, where Student is the class. When the object of the same class type is passed by value as an argument.


2 Answers

As a rule of thumb: If you have to manually manage resources, wrap each into its own object.

Put that char* into its own object with a proper copy constructor and let the compiler do the copy constructor for A. Note that this also deals with assignment and destruction, which you haven't mentioned in your question, but need to be dealt with nevertheless.
The standard library has several types to pick from for that, among them std::string and std::vector<char>.

like image 194
sbi Avatar answered Sep 23 '22 14:09

sbi


Replace char* with std::string.

like image 25
fredoverflow Avatar answered Sep 21 '22 14:09

fredoverflow