A copy constructor for a class created as pointer is as simple as this ?


1
2
3
4
5
6
7
8
9
10
11
struct My_classA {
 vector<another_class> V1;
 vector<another_class> V2;
 MY_classA(const My_classA *ref)
 {
  this->V1 = ref->V1;
  this->V2 = ref->V2;
  .....
 }

}


I have :
1
2
3
4
  an_instance_of_my_classA = new My_classA;
  do things  with   an_instance_of_my_classA .....
  a_copy_of_my_classA = new My_classA_copy(My_classA);
  do things with    an_instance_of_my_classA

At this point can I'm sure that
a_copy_of_my_classA are the original data? Have I any risk of memory-pointers problem ?
It seems to work but I'm surprised that it was so easy.....
I need your opinion , thanks,





Yep that works. Basically what you really have to watch out for are pointer members.
Well for starters that is not a copy constructor. A copy constructor takes a const reference, not a const pointer.

Furthermore, by using new like that you are leaking memory because you never delete the object you new'd.

Lastly, you don't even need to provide your own copy constructor here. The one the compiler provides will work just fine. You only need to provide your own if you are doing some weird memory management or need to do something special when an object is copied.

Here's what you should be doing:
1
2
3
4
5
6
7
8
9
10
11
struct My_classA {
 vector<another_class> V1;
 vector<another_class> V2;
 MY_classA(const My_classA &ref)  // <- take a ref &, not a pointer *
 {
  this->V1 = ref.V1;  // <- "ref." not "ref->"
  this->V2 = ref->V2; //  same
  .....  // but again -- you really don't need this ctor at all.  You could get rid of it
 }

}

1
2
3
My_classA foo;  // this constructs an instance named 'foo'
My_classA bar = foo;  // this copies it to bar
My_classA baz(foo);  // this does the same thing as the above line -- just alternate syntax 


Notice how I never use new. new is only needed if you want to dynamically create an object -- and even then you would assign it to a pointer not an object.
Ufff.
I need to use pointers, I'm passing objects betwen classes.
So, I detele the const keyword of my constructor,

1
2
3
4
5
6
    MY_classA(My_classA *ref)
 {
  this->V1 = ref->V1;
  this->V2 = ref->V2;
  .....
 }


this->V1 = ref->V1; is doing a copy or not ?
I think yes because the original and copy objects has different values (but it could be an error of my debug version... )

What do you think ?

Thanks.
I need to use pointers, I'm passing objects betwen classes.


Not like this you don't. It doesn't make logical sense for a constructor to construct itself from a pointer to an object. They all take full objects.

For example:
1
2
3
int a = new int; // fails
string b = new string;  // fails
My_classA c = new My_classA;  // should fail... but you want this to work?  That's confusing 


If you have a pointer, you can get a reference by dereferencing it. Example:

1
2
3
My_classA* ptr = /*whatever*/

My_classA copy = *ptr;  // copies whatever ptr points to 


This uses the normal copy constructor (the one that takse a reference, not a pointer). And is how you should be doing it.

this->V1 = ref->V1; is doing a copy or not ?


Yes that is copying, but the problem is not so much with the constructor body so much as it's with your overall design. You're trying to make a constructor that makes no logical sense.


EDIT:

and of course, just to reiterate...

My_classA c = new My_classA;

This code leaks memory. Even if you write the ctor correctly.
Last edited on
I have not My_classA c = new My_classA,
I have a private My_classA *c, declared as private for the main class, i'm going to use it troughout various functions along my main class.

In adittion I pass the 'c' instance to antoher classes, this lasts does some thing with it.
In this special case (copy -clone) I need to copy and temporary 'undo' object, this is the reason to use the copy-contructor.


And at the end of all I delete it. So I think I have not memory leaks .
Last edited on
I have not My_classA c = new My_classA,


Okay. You had that in your original post hence my confusion.

I have a private My_classA *c, declared as private for the main class, i'm going to use it troughout various functions along my main class.


You still don't need the abnormal constructor form. Even if you have a pointer, you don't pass that pointer to the ctor. You dereference it.

1
2
3
MyClass* c = ...;

MyClass* undocopy = new MyClass(*c); // <-  note:  *c 


By doing '*c' instead of just 'c' you are passing a reference instead of a pointer. This means you can use the "normal" copy constructor, which further means you don't have to write one yourself.
ok, thank you very much
Topic archived. No new replies allowed.