what is Deep Copy, shallow copy and bitwises copy.?

J

Jim Langston

Roland Pibinger said:
PlayerList.push_back( Player ); // Ooops, why would you duplicate
your CPlayers??

I'm actually using very similar code in one of my programs. The reason is,
the Player is logging in with user name and I need to know if the user
exists. In this case I am loading the player with ostream >> Player then
checking to see if it's valid (passwords match, user is not banned, etc...).
If, and only if, it is a valid player with correct cridentials do I want to
add this player to my list of players.

The other option is to store a CPlayer* instead of a CPlayer in the vector,
but there is no reason to use pointers just so I won't have to use a copy
constructor.

That's why.
 
R

Roland Pibinger

I'm actually using very similar code in one of my programs. The reason is,
the Player is logging in with user name and I need to know if the user
exists. In this case I am loading the player with ostream >> Player then
checking to see if it's valid (passwords match, user is not banned, etc...).
If, and only if, it is a valid player with correct cridentials do I want to
add this player to my list of players.

Ok, let's continue your example above:

std::vector<CPlayer> PlayerList;
CPlayer Player;
Player.LoadData( SomeFile );
PlayerList.push_back( Player );

PlayerList[0].run();
Player.stop()

Is your player now running or stopped?

Best wishes,
Roland Pibinger
 
J

Jim Langston

Roland Pibinger said:
I'm actually using very similar code in one of my programs. The reason
is,
the Player is logging in with user name and I need to know if the user
exists. In this case I am loading the player with ostream >> Player then
checking to see if it's valid (passwords match, user is not banned,
etc...).
If, and only if, it is a valid player with correct cridentials do I want
to
add this player to my list of players.

Ok, let's continue your example above:

std::vector<CPlayer> PlayerList;
CPlayer Player;
Player.LoadData( SomeFile );
PlayerList.push_back( Player );

PlayerList[0].run();
Player.stop()

Is your player now running or stopped?

After the player is pushed onto the vector I don't use it anymore. And
those are 2 different instances of vectors, not the same one. To answer
your question, one copy of the player is running, one copy of the player is
stopped.

But I'm smart enough not to use the player that was loaded into the vector
from the original isntance.

Your question is similar to saying this:

int X = 10;
int Y = X;

X = 5;
Y = 7;

Is my interger 5 or 7?
 
J

Jim Langston

Roland Pibinger said:
5 and 7 are values, your player is an object. That's the difference.
For a short description of the difference between values and objects
(entities) see:
http://www.two-sdg.demon.co.uk/curbralan/papers/ObjectsOfValue.pdf

My player has the value of "Serpardum" also. And, per your example, a value
of Running_ or Stopped_.

And I can come up with many many more reasons to want to copy an object.

Say I'm writing a graphical tool that contains 3 dimentional objects. And
the user wants to copy a 3d object on the screen and make 2 of them. In my
program I may store these in classes, and would simply copy the existing
object to a new one.

Just because *you* can't think of any good reasons to copy objects doesn't
mean they don't exist.
 
R

Roland Pibinger

Say I'm writing a graphical tool that contains 3 dimentional objects. And
the user wants to copy a 3d object on the screen and make 2 of them. In my
program I may store these in classes, and would simply copy the existing
object to a new one.

The keyword 'class' doesn't indicate whether you have a value type or
an object type. Your 3d object is most probably a value type, e.g.

struct Point {
int x;
int y;
int z;
};
Just because *you* can't think of any good reasons to copy objects doesn't
mean they don't exist.

<offtopic>
Recently Qt announced a Java library for their framework
(http://doc.trolltech.com/qtjambi-1.0/qtjambi-index.html).
Compare what _they_ consider value types
(http://doc.trolltech.com/qtjambi-1.0/qtjambi-value-types.html) vs.
what they consider object types
(http://doc.trolltech.com/qtjambi-1.0/qtjambi-object-types.html).
</offtopic>

Best wishes,
Roland Pibinger
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top