STL & C++ constructors and operators

Hi guys,

in some STL documentation I read that each element which shall be used inside a STL-container (e.g. vector) must have a copy constructor and an assignment operator (operator=).
The same documentation states, that these two functions are automatically generated by the C++ compiler.

Is this true? Are all member variables automatically "copied" by these default operations?

Regards
  Mirko
MirkoAsked:
Who is Participating?
 
yonatConnect With a Mentor Commented:
1. Only the pointer itself is copied, not the value it points to. If you have a pointer pX as member, you usually don't want a simple
    this.pX = new X(*r.pX);
But instead:
    this.pX = r.pX->clone();
(or something to that effect).

2. If you have a user defined type, its operator= (or copy constructor) will get called.
0
 
yonatCommented:
Yes, provided you did not write any copy constructor and assignment operator of your own.
0
 
MirkoAuthor Commented:
Hi yonat,

And what about pointers? Are the values also copied?
And what about custom types? I have a struct for which I defined an "operator=". Will this struct also be automatically copied correctly?
Please post your comment as "Answer" so that I can give you the credits...

Regards
  Mirko
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
monkey3Commented:
i agree with yonat

with pointers if the object has a relevant copy constructor taking a pointer and returning a pointer, then its ok, other wise you will have to dereference the pointers.

for a custom struct, you will have to write the copy code yourself.
0
 
MirkoAuthor Commented:
Thank you.
0
 
monkey3Commented:
yonat, have any ideas with regards to recording sound.

see question:-

recording sound - easy way to.
0
All Courses

From novice to tech pro — start learning today.