468,512 Members | 1,445 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

Dynamic Storage allocation - copy constructor ---C++

Hi All,

Generally copy constructor is used to create a new object from existing object.

My question is,

Do i need to allocated memory to the pointer member variables in copy constructor. Or will they share??
Sep 12 '06 #1
2 3422
deepha
4
Hai,
By default the pointer variable will use the same memory of the existing object. But if u what you can over load the copy constructor.

If u are not doing so, when ur program exists you will end up in deleting the same memory twice.
Sep 12 '06 #2
Banfa
9,056 Expert Mod 8TB
If u are not doing so, when ur program exists you will end up in deleting the same memory twice.
Which is a very bad thing to do.

In general if you class does not contain any allocated pointers it is often OK to just use the default copy constructor supplied by C++. If your class contains pointers that you have allocated memory to then you will almost certainly need to override the default copy constructor and write your own to allocate memory for the new class.
Sep 12 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Bob | last post: by
4 posts views Thread by William Payne | last post: by
14 posts views Thread by MSR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.