-1

One of the purposes of a copy constructor is so that pointers declared in a copy point to their respective members, and not simply back to the originals members, but how exactly is this implemented?

say your constructor is:

foo::foo(int i)
{
    blah = i;
    bar = new whatever; //bar is a pointer to a whatever
}

so what should the copy constructor implementation look like? is the only thing you have to put in it something like:

bar = this->whatever;

or

bar = whatever;

or should it contain everything the normal constructor does?

Dollarslice
  • 9,917
  • 22
  • 59
  • 87
  • This thread explains how it needs to be implemented. [Rule of Three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – Mahesh Dec 07 '11 at 21:15
  • 1
    If I was going to write a copy constructor for a class, I'd definitely want to see the definition of that class. – CB Bailey Dec 07 '11 at 21:31

3 Answers3

1

It entirely depends on what a foo is, and its relation to a whatever.

Possible options are:

Sharing the object

foo::foo(const foo& other) : blah(other.blah), bar(other.bar)
{ }

Creating another object

foo::foo(const foo& other) : blah(other.blah), bar(new whatever)
{ }

Creating a new copy of the object

foo::foo(const foo& other) : blah(other.blah), bar(new whatever(*other.bar))
{ }
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • but with all of these, all of the original foo's members are copied unless you do something specific with them? – Dollarslice Dec 07 '11 at 21:37
  • or do you have to list every single member you want to copy? – Dollarslice Dec 07 '11 at 21:41
  • So `foo` has more than two members? :-) *You* have to decide what it means to create a copy. If you add a copy constructor, that decides exactly how the object is copied. The compiler only creates a default (member wise) copy if you don't. – Bo Persson Dec 07 '11 at 21:42
  • so if foo had many members and you want the copy to have the same, you have to list them ALL in the copy constructor. is that right? what about the member functions? are they all present by default? – Dollarslice Dec 07 '11 at 23:08
-1

No, copy constructor is about copying members of the object, it doesn't have much to do with pointers, unless the members are. Like if you have an object point with x and y members, you would make a copy constructor

point(const point& p) : x(p.x), y(p.y) { }

and it may have another constructor like

point(int x_,int y_) : x(x_), y(y_) { }

If you have a pointer member, it depends on the object and how you want to handle it — you may want to copy the pointer or create another pointer, etc. Depends on the data it is pointing to.

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
  • I have a pointer pointing to an member, if I let the compiler create a default copy constructor then the pointer in the copied class points to the original member. I want it to point to it's own member. – Dollarslice Dec 07 '11 at 21:13
  • For point? My best guess is coordinates, but it doesn't matter. It could be a complex number with real and imaginary parts, it could be a string, it could be a person and person's iq, it could be a developer with the list of programming languages he speaks fluently, etc. – Michael Krelin - hacker Dec 07 '11 at 21:14
  • Isn't that exactly what the default copy constructor does? Why would I do that, possibly introducing a bug when I added another member and forgot to change the copy constructor accordingly? – pezcode Dec 07 '11 at 21:15
  • It depends on the object. You may want to add some lifetime history for the object or whatever… You do not necessarily want to copy all members. – Michael Krelin - hacker Dec 07 '11 at 21:19
-1

If you don't have a copy constructor, a default one will be created for you which does a shallow copy, where each member variable of one object is simply assigned to the other, as opposed to a deep copy.

Therefore if you have a pointer to a dynamically allocated buffer, then the default copy constructor is going to simply assign that pointer to the copy's pointer, rather than creating it's own new buffer to point to and copying the contents of the buffer into the new one. eg.

class DynamicIntArray {
private:
   int *_array;
   size_t _size;

public:
   DynamicIntArray(size_t size) : _array(new int[size]), _size(size) { }

   DynamicIntArray (const DynamicIntArray &d) // copy constructor
   {
      delete[] _array;
      _array = new int[d._size];
      _size = d._size;
      std::copy(_array, d._array, d._array + d._size);
   }

   /* destructor, assignment operator, etc */
};

If you didn't create a default copy constructor, then the default one created would simply assign d._array to _array, which would cause serious problems. ie. Instead of the above, it would do:

_array = d._array;
_size = d._size;

Note that if you have a copy constructor, you should probably have an assignment operator and a destructor (look up the rule-of-three).

AusCBloke
  • 18,014
  • 6
  • 40
  • 44
  • it will assign the original pointer to the copy? are you sure? – Dollarslice Dec 07 '11 at 21:32
  • @SirYakalot: If you use the default copy constructor, then it will simply do `_array = d.array;`. I think you can see the problem with that. – AusCBloke Dec 07 '11 at 21:34
  • Is there something wrong with this answer? I thought it answered the OP's question of how a copy constructor is implemented. :) – AusCBloke Dec 07 '11 at 23:27