Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > what is Deep Copy, shallow copy and bitwises copy.?

Reply
Thread Tools

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

 
 
Jim Langston
Guest
Posts: n/a
 
      08-31-2006
"Roland Pibinger" <> wrote in message
news:...
> On Thu, 31 Aug 2006 02:21:59 -0700, "Jim Langston"
> <> wrote:
>>std::vector<CPlayer> PlayerList;
>>CPlayer Player;
>>Player.LoadData( SomeFile );
>>PlayerList.push_back( Player ); // Ooops, need copy constuctor for that

>
> 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.


 
Reply With Quote
 
 
 
 
Roland Pibinger
Guest
Posts: n/a
 
      09-01-2006
On Thu, 31 Aug 2006 14:38:25 -0700, "Jim Langston"
<> wrote:
>"Roland Pibinger" <> wrote in message
>news:...
>> On Thu, 31 Aug 2006 02:21:59 -0700, "Jim Langston"
>> <> wrote:
>>>std::vector<CPlayer> PlayerList;
>>>CPlayer Player;
>>>Player.LoadData( SomeFile );
>>>PlayerList.push_back( Player ); // Ooops, need copy constuctor for that

>>
>> 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.


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
 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      09-01-2006

"Roland Pibinger" <> wrote in message
news:...
> On Thu, 31 Aug 2006 14:38:25 -0700, "Jim Langston"
> <> wrote:
>>"Roland Pibinger" <> wrote in message
>>news:...
>>> On Thu, 31 Aug 2006 02:21:59 -0700, "Jim Langston"
>>> <> wrote:
>>>>std::vector<CPlayer> PlayerList;
>>>>CPlayer Player;
>>>>Player.LoadData( SomeFile );
>>>>PlayerList.push_back( Player ); // Ooops, need copy constuctor for that
>>>
>>> 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.

>
> 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?





 
Reply With Quote
 
Roland Pibinger
Guest
Posts: n/a
 
      09-01-2006
On Fri, 1 Sep 2006 01:16:30 -0700, "Jim Langston"
<> wrote:
>Your question is similar to saying this:
>
>int X = 10;
>int Y = X;
>
>X = 5;
>Y = 7;
>
>Is my interger 5 or 7?


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/curbr...ctsOfValue.pdf

Best wishes,
Roland Pibinger
 
Reply With Quote
 
loufoque
Guest
Posts: n/a
 
      09-01-2006
Roland Pibinger wrote :

> 5 and 7 are values, your player is an object.


You seem to confuse objects and entities.

 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      09-01-2006
"Roland Pibinger" <> wrote in message
news:...
> On Fri, 1 Sep 2006 01:16:30 -0700, "Jim Langston"
> <> wrote:
>>Your question is similar to saying this:
>>
>>int X = 10;
>>int Y = X;
>>
>>X = 5;
>>Y = 7;
>>
>>Is my interger 5 or 7?

>
> 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/curbr...ctsOfValue.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.


 
Reply With Quote
 
Roland Pibinger
Guest
Posts: n/a
 
      09-01-2006
On Fri, 1 Sep 2006 11:56:48 -0700, "Jim Langston"
<> wrote:
>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...bi-index.html).
Compare what _they_ consider value types
(http://doc.trolltech.com/qtjambi-1.0...lue-types.html) vs.
what they consider object types
(http://doc.trolltech.com/qtjambi-1.0...ct-types.html).
</offtopic>

Best wishes,
Roland Pibinger
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to implement a deep copy or shallow copy? shuisheng C++ 4 12-17-2006 08:40 AM
is dict.copy() a deep copy or a shallow copy Alex Python 2 09-05-2005 07:01 AM
How to overload operator= to have both deep copy and shallow copy semantics bluekite2000@gmail.com C++ 1 06-24-2005 05:28 PM
deep and shallow copy Tony Johansson C++ 5 05-19-2005 04:13 PM
deep/shallow copy - constructor v Object.copy() VisionSet Java 8 04-29-2004 10:41 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57