Copy constructor
Hi All,
I know that if a have class member that its being allocated dynamically
I have to implement copy constructor and use deep copy (usually).
Can I use default copy constructor if I have only staticly allocated memebr?
Say for example I have the following
1 2 3 4 5 6
|
class A
{
int x;
int y[10];
};
|
Can I use the compiler's copy constructor? and if so why I don't need to implement deep copy in order to copy array y?
Thank u in advance
Can I use the compiler's copy constructor? and if so why I don't need to implement deep copy in order to copy array y? |
Hmm... because there's bitwise copying involved. Which means that data
x and
y are copied entirely.
When you use dynamic allocation, and you have a member
int *z;
all that is copied is the value of
z.
However
z is a pointer, so its value represents a memory address. So what is copied is a memory address, not the contents of the pointed-to memory.
Edit: I may be totally wrong. I'll test and see.
http://forums.codeguru.com/showthread.php?500827-C-Does-implicitly-defined-copy-constructor-bitwise-copy
Last edited on
Topic archived. No new replies allowed.