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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
yonatCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.