S
saxenavaibhav17
what is Deep Copy, Shallow copy and Bitwise copy, Memberwise copy?
and what is the difference between them?
pls help
vaibhav
and what is the difference between them?
pls help
vaibhav
what is Deep Copy, Shallow copy and Bitwise copy, Memberwise copy?
and what is the difference between them?
What is a homework question? See this FAQ:
BTW, copy constructors almost never need to be implemented but should
be made private (without implementation).
memcpy(&obj2,&obj1,sizeof obj2); /* Shallow / Memberwise */
Frederick said:(3) Deep Copy
class MyClass {
int *p;
public:
MyClass() : p(new int) {}
MyClass(MyClass const &) : p(new int) {}
(1) Shallow Copy and Memberwise copy (They're equivalent)
why? Doesn't copy construction very often make semantic sense?
OTOH, objects (in the OO-sense) typically are non-copyable
Roland Pibinger wrote :
Please don't confuse your personal beliefs with programming theory.
I see no reason why objects would typically be non-copyable.
Especially in C++ which is based on copy semantics.
On the contrary, most objects should be copyable, unless they manage
some kind of unique resource (sockets, files, ...).
Moreover, if you want to make your object non-copyable clearly says so
instead of saying "write a private constructor". Actually, inheriting
from boost::noncopyable is probably clearer.
Roland said:Why should objects be duplicated? What should it mean? Eg.
Person dilbert;
Person wally (dilbert); // ???
Roland said:I don't.
Why should objects be duplicated? What should it mean? Eg.
Person dilbert;
Person wally (dilbert); // ???
and reference/pointer semantics ...
Objects have identity. The can be referenced by multiple refences but
it hardly makes sense to duplicate them. (Even in OO languages like
Java objects are hardly ever 'cloned' even though a Clonable interface
exists).
Sheep dolly (bill);
Anyway, copy-constructors are also used for moving objects
around in memory. For example, returning an object from a
function, or storing an object in a container than can re-allocate
its memory. The new object is copy-constructed from the
original and the original is then destroyed.
Roland said:What you can do in C++ is different from what is desirable. Having
duplicate objects (objects (in the OO sense), not values) of the same
entity in your program is harldy ever a Good Thing (at least I can
quickly think of no case where it is).
BTW, copy constructors almost never need to be implemented but should
be made private (without implementation).
Roland Pibinger said:What you can do in C++ is different from what is desirable. Having
duplicate objects (objects (in the OO sense), not values) of the same
entity in your program is harldy ever a Good Thing (at least I can
quickly think of no case where it is).
Keep in mind, though, that C++ supports various other viable styles of
programming, where your orignial remark
hardly is applicable. I find that value semantics is the most appropriate
model for most of my programming and copy constructors need to be defined
in most cases.
std::vector<CPlayer> PlayerList;
CPlayer Player;
Player.LoadData( SomeFile );
PlayerList.push_back( Player ); // Ooops, need copy constuctor for that
Roland said:What I mean is that copy constructors are either trivial for values
(and therfore need not be implemented) or should be made private (and
left unimplemented) for objects.
Kai-Uwe Bux said:Keep in mind, though, that C++ supports various other viable styles of
programming, where your orignial remark
hardly is applicable. I find that value semantics is the most appropriate
model for most of my programming and copy constructors need to be defined
in most cases. In the vast variety of what can be done well in C++, strict
OO programming is but a small fraction; and advice targeted to OO design
proper is of very limited scope when talking C++.
Best
Kai-Uwe Bux
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.