473,802 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Instances effecting each other?

My previous thread got very large so here is my point again, but a better
example of my problem:

SceneChair chair = (SceneChair)_ob jMan.GetObject( (int)ObjectID.S eats);

chair.Position = GetSeatPosition (1);

chair.FrameNum = 1;

_objMan.AddToRe nderStack(chair );

SceneChair chair2 = new SceneChair();

chair2 = chair;

chair2.Position = GetSeatPosition (3);

chair2.FrameNum = 2;

_objMan.AddToRe nderStack(chair 2);

Ok in the above code when i debug, at the line chair2.Position both
instances are equal in data as you would expect.

Now when i go to the final _objMan.AddToRe nderStack line both the chair
instance and the chair2 instance framenumbers become 2. But i only changed
chair2's one so chair being a differenct instance shouldnt be effected
right?

The SceneChair chair2 = new SceneChair(); should have created a new memory
space and then when i changed chair2's framenum the memory allocated for
chair2 shoudl change while chair's remains the same. Am i right? So why do
they both change later on??? This is why when i add it to my stack they are
equal, as it is changing them as i said before while on the stack as if it
were the same instance.
May 1 '06
18 1297
Yes, if those are all the member variables you have in the class, that
will work nicely.

You would declare the method as:

public myObj Clone()
Daniel wrote:
But now i am not sure how to create a new instance to do that with, i always
thought 'new' did this.

Is this right?

Clone()
{
Object myObj = new myObj();

myObj.x = this.x;
myObj.y = this.y;

return myObj;
}

Would the above return a clone not a reference?
"Göran Andersson" <gu***@guffa.co m> wrote in message
news:uN******** ******@TK2MSFTN GP03.phx.gbl...
Daniel wrote:
Thats the answer i was looking for.

I made that bit of code as a basic example buti think that is what is
happening anyway.

So in that case how can i take an instance in an array and create a new
instance with the same data?

I want to do this:

Object newInstance = _myList[1];

Where newInstance when changed does not affect _myList[1] after.

As Lebesgue said, you should clone the object. Basically that means
creating a new instance and copy all the data to it.
I always thought by using the new operator it solved this by allocating
new memory i didnt realise that the '=' sign copied the reference and not
the actual data.

Could you show me how to do this? What i dont understand is i do this
kind of thing all the time, the only difference here is that i am using
List instead of array, i preumse arrays create new instances and lists
use the reference?

No. Instances are never created automatically. You must have been lucky in
the past. That, or you have a lot of potential bugs lurking in your
code...
"Göran Andersson" <gu***@guffa.co m> wrote in message
news:%2******** ********@TK2MSF TNGP05.phx.gbl. ..
Daniel wrote:
> My previous thread got very large so here is my point again, but a
> better example of my problem:
>
> SceneChair chair = (SceneChair)_ob jMan.GetObject( (int)ObjectID.S eats);
>
> chair.Position = GetSeatPosition (1);
>
> chair.FrameNum = 1;
>
> _objMan.AddToRe nderStack(chair );
>
> SceneChair chair2 = new SceneChair();
>
> chair2 = chair;
>
> chair2.Position = GetSeatPosition (3);
>
> chair2.FrameNum = 2;
>
> _objMan.AddToRe nderStack(chair 2);
>
>
>
> Ok in the above code when i debug, at the line chair2.Position both
> instances are equal in data as you would expect.
Not only in data. They are the same instance. You are not copying the
data of the object, but the reference to the object.

> Now when i go to the final _objMan.AddToRe nderStack line both the chair
> instance and the chair2 instance framenumbers become 2. But i only
> changed
> chair2's one so chair being a differenct instance shouldnt be effected
> right?
No, it isn't a different instance.

> The SceneChair chair2 = new SceneChair(); should have created a new
> memory
> space
Yes, it does. But you overwrite the reference to that new object, so
it's thrown away and never used.

> and then when i changed chair2's framenum the memory allocated for
> chair2 shoudl change while chair's remains the same. Am i right? So why
> do
> they both change later on???
Not later on. As there is only one instance, it changes instantly when
you change it.

> This is why when i add it to my stack they are
> equal, as it is changing them as i said before while on the stack as if
> it
> were the same instance.
That is because they *are* the same instance.

May 1 '06 #11
No, just using the constructor will do fine.

Daniel wrote:
Sorry following on from last example.....do i need to do this to make th
enew insatce?

object newObject = Activator.Creat eInstance(this. GetType());

"Göran Andersson" <gu***@guffa.co m> wrote in message
news:uN******** ******@TK2MSFTN GP03.phx.gbl...
Daniel wrote:
Thats the answer i was looking for.

I made that bit of code as a basic example buti think that is what is
happening anyway.

So in that case how can i take an instance in an array and create a new
instance with the same data?

I want to do this:

Object newInstance = _myList[1];

Where newInstance when changed does not affect _myList[1] after.

As Lebesgue said, you should clone the object. Basically that means
creating a new instance and copy all the data to it.
I always thought by using the new operator it solved this by allocating
new memory i didnt realise that the '=' sign copied the reference and not
the actual data.

Could you show me how to do this? What i dont understand is i do this
kind of thing all the time, the only difference here is that i am using
List instead of array, i preumse arrays create new instances and lists
use the reference?

No. Instances are never created automatically. You must have been lucky in
the past. That, or you have a lot of potential bugs lurking in your
code...
"Göran Andersson" <gu***@guffa.co m> wrote in message
news:%2******** ********@TK2MSF TNGP05.phx.gbl. ..
Daniel wrote:
> My previous thread got very large so here is my point again, but a
> better example of my problem:
>
> SceneChair chair = (SceneChair)_ob jMan.GetObject( (int)ObjectID.S eats);
>
> chair.Position = GetSeatPosition (1);
>
> chair.FrameNum = 1;
>
> _objMan.AddToRe nderStack(chair );
>
> SceneChair chair2 = new SceneChair();
>
> chair2 = chair;
>
> chair2.Position = GetSeatPosition (3);
>
> chair2.FrameNum = 2;
>
> _objMan.AddToRe nderStack(chair 2);
>
>
>
> Ok in the above code when i debug, at the line chair2.Position both
> instances are equal in data as you would expect.
Not only in data. They are the same instance. You are not copying the
data of the object, but the reference to the object.

> Now when i go to the final _objMan.AddToRe nderStack line both the chair
> instance and the chair2 instance framenumbers become 2. But i only
> changed
> chair2's one so chair being a differenct instance shouldnt be effected
> right?
No, it isn't a different instance.

> The SceneChair chair2 = new SceneChair(); should have created a new
> memory
> space
Yes, it does. But you overwrite the reference to that new object, so
it's thrown away and never used.

> and then when i changed chair2's framenum the memory allocated for
> chair2 shoudl change while chair's remains the same. Am i right? So why
> do
> they both change later on???
Not later on. As there is only one instance, it changes instantly when
you change it.

> This is why when i add it to my stack they are
> equal, as it is changing them as i said before while on the stack as if
> it
> were the same instance.
That is because they *are* the same instance.

May 1 '06 #12
No, it is not right.
C# doesn't have late binding (did you come from VB?) and Object doesn't have
any member called x or y, so this would not work.

//Please note that if x and y are reference types, you will want to clone
them as well.

public object Clone()
{
myObj instance = new myObj();
instance.x = this.x;

return instance;
}

myObj.x
Would the above return a clone not a reference?
it will definitely return a reference to a new object (to the clone). I'm
afraid you do not fully understand what an object and what a reference is...

"Daniel" <Da*****@vestry online.com> wrote in message
news:ef******** ******@TK2MSFTN GP02.phx.gbl... But now i am not sure how to create a new instance to do that with, i
always thought 'new' did this.

Is this right?

Clone()
{
Object myObj = new myObj();

myObj.x = this.x;
myObj.y = this.y;

return myObj;
}

Would the above return a clone not a reference?
"Göran Andersson" <gu***@guffa.co m> wrote in message
news:uN******** ******@TK2MSFTN GP03.phx.gbl...
Daniel wrote:
Thats the answer i was looking for.

I made that bit of code as a basic example buti think that is what is
happening anyway.

So in that case how can i take an instance in an array and create a new
instance with the same data?

I want to do this:

Object newInstance = _myList[1];

Where newInstance when changed does not affect _myList[1] after.


As Lebesgue said, you should clone the object. Basically that means
creating a new instance and copy all the data to it.
I always thought by using the new operator it solved this by allocating
new memory i didnt realise that the '=' sign copied the reference and
not the actual data.

Could you show me how to do this? What i dont understand is i do this
kind of thing all the time, the only difference here is that i am using
List instead of array, i preumse arrays create new instances and lists
use the reference?


No. Instances are never created automatically. You must have been lucky
in the past. That, or you have a lot of potential bugs lurking in your
code...
"Göran Andersson" <gu***@guffa.co m> wrote in message
news:%2******** ********@TK2MSF TNGP05.phx.gbl. ..
Daniel wrote:
> My previous thread got very large so here is my point again, but a
> better example of my problem:
>
> SceneChair chair = (SceneChair)_ob jMan.GetObject( (int)ObjectID.S eats);
>
> chair.Position = GetSeatPosition (1);
>
> chair.FrameNum = 1;
>
> _objMan.AddToRe nderStack(chair );
>
> SceneChair chair2 = new SceneChair();
>
> chair2 = chair;
>
> chair2.Position = GetSeatPosition (3);
>
> chair2.FrameNum = 2;
>
> _objMan.AddToRe nderStack(chair 2);
>
>
>
> Ok in the above code when i debug, at the line chair2.Position both
> instances are equal in data as you would expect.
Not only in data. They are the same instance. You are not copying the
data of the object, but the reference to the object.

> Now when i go to the final _objMan.AddToRe nderStack line both the
> chair
> instance and the chair2 instance framenumbers become 2. But i only
> changed
> chair2's one so chair being a differenct instance shouldnt be effected
> right?
No, it isn't a different instance.

> The SceneChair chair2 = new SceneChair(); should have created a new
> memory
> space
Yes, it does. But you overwrite the reference to that new object, so
it's thrown away and never used.

> and then when i changed chair2's framenum the memory allocated for
> chair2 shoudl change while chair's remains the same. Am i right? So
> why do
> they both change later on???
Not later on. As there is only one instance, it changes instantly when
you change it.

> This is why when i add it to my stack they are
> equal, as it is changing them as i said before while on the stack as
> if it
> were the same instance.
That is because they *are* the same instance.

May 1 '06 #13
It was an example Lebasque, presume object was something else. Its the
concept i am after.

Cheers Goran that's solved my problem.

I come from a c++ background where pointers and references made it clear
when you were using ref and not. Hence my confusion. Got it now, thanks for
all your help guys.

"Lebesgue" <le******@gmail .com> wrote in message
news:OO******** ******@TK2MSFTN GP04.phx.gbl...
No, it is not right.
C# doesn't have late binding (did you come from VB?) and Object doesn't
have any member called x or y, so this would not work.

//Please note that if x and y are reference types, you will want to clone
them as well.

public object Clone()
{
myObj instance = new myObj();
instance.x = this.x;

return instance;
}

myObj.x
Would the above return a clone not a reference?


it will definitely return a reference to a new object (to the clone). I'm
afraid you do not fully understand what an object and what a reference
is...

"Daniel" <Da*****@vestry online.com> wrote in message
news:ef******** ******@TK2MSFTN GP02.phx.gbl...
But now i am not sure how to create a new instance to do that with, i
always thought 'new' did this.

Is this right?

Clone()
{
Object myObj = new myObj();

myObj.x = this.x;
myObj.y = this.y;

return myObj;
}

Would the above return a clone not a reference?
"Göran Andersson" <gu***@guffa.co m> wrote in message
news:uN******** ******@TK2MSFTN GP03.phx.gbl...
Daniel wrote:
Thats the answer i was looking for.

I made that bit of code as a basic example buti think that is what is
happening anyway.

So in that case how can i take an instance in an array and create a new
instance with the same data?

I want to do this:

Object newInstance = _myList[1];

Where newInstance when changed does not affect _myList[1] after.

As Lebesgue said, you should clone the object. Basically that means
creating a new instance and copy all the data to it.

I always thought by using the new operator it solved this by allocating
new memory i didnt realise that the '=' sign copied the reference and
not the actual data.

Could you show me how to do this? What i dont understand is i do this
kind of thing all the time, the only difference here is that i am using
List instead of array, i preumse arrays create new instances and lists
use the reference?

No. Instances are never created automatically. You must have been lucky
in the past. That, or you have a lot of potential bugs lurking in your
code...

"Göran Andersson" <gu***@guffa.co m> wrote in message
news:%2******** ********@TK2MSF TNGP05.phx.gbl. ..
> Daniel wrote:
>> My previous thread got very large so here is my point again, but a
>> better example of my problem:
>>
>> SceneChair chair =
>> (SceneChair)_ob jMan.GetObject( (int)ObjectID.S eats);
>>
>> chair.Position = GetSeatPosition (1);
>>
>> chair.FrameNum = 1;
>>
>> _objMan.AddToRe nderStack(chair );
>>
>> SceneChair chair2 = new SceneChair();
>>
>> chair2 = chair;
>>
>> chair2.Position = GetSeatPosition (3);
>>
>> chair2.FrameNum = 2;
>>
>> _objMan.AddToRe nderStack(chair 2);
>>
>>
>>
>> Ok in the above code when i debug, at the line chair2.Position both
>> instances are equal in data as you would expect.
> Not only in data. They are the same instance. You are not copying the
> data of the object, but the reference to the object.
>
>> Now when i go to the final _objMan.AddToRe nderStack line both the
>> chair
>> instance and the chair2 instance framenumbers become 2. But i only
>> changed
>> chair2's one so chair being a differenct instance shouldnt be
>> effected
>> right?
> No, it isn't a different instance.
>
>> The SceneChair chair2 = new SceneChair(); should have created a new
>> memory
>> space
> Yes, it does. But you overwrite the reference to that new object, so
> it's thrown away and never used.
>
>> and then when i changed chair2's framenum the memory allocated for
>> chair2 shoudl change while chair's remains the same. Am i right? So
>> why do
>> they both change later on???
> Not later on. As there is only one instance, it changes instantly when
> you change it.
>
>> This is why when i add it to my stack they are
>> equal, as it is changing them as i said before while on the stack as
>> if it
>> were the same instance.
> That is because they *are* the same instance.


May 1 '06 #14
When I look closer at your code, you have mixed up the class names. Here
is what it should look like:

public myObj Clone() {
myObj obj = new myObj();
obj.x = this.x;
obj.y = this.y;
return obj;
}

Göran Andersson wrote:
Yes, if those are all the member variables you have in the class, that
will work nicely.

You would declare the method as:

public myObj Clone()
Daniel wrote:
But now i am not sure how to create a new instance to do that with, i
always thought 'new' did this.

Is this right?

Clone()
{
Object myObj = new myObj();

myObj.x = this.x;
myObj.y = this.y;

return myObj;
}

Would the above return a clone not a reference?
"Göran Andersson" <gu***@guffa.co m> wrote in message
news:uN******** ******@TK2MSFTN GP03.phx.gbl...
Daniel wrote:
Thats the answer i was looking for.

I made that bit of code as a basic example buti think that is what
is happening anyway.

So in that case how can i take an instance in an array and create a
new instance with the same data?

I want to do this:

Object newInstance = _myList[1];

Where newInstance when changed does not affect _myList[1] after.
As Lebesgue said, you should clone the object. Basically that means
creating a new instance and copy all the data to it.

I always thought by using the new operator it solved this by
allocating new memory i didnt realise that the '=' sign copied the
reference and not the actual data.

Could you show me how to do this? What i dont understand is i do
this kind of thing all the time, the only difference here is that i
am using List instead of array, i preumse arrays create new
instances and lists use the reference?
No. Instances are never created automatically. You must have been
lucky in the past. That, or you have a lot of potential bugs lurking
in your code...

"Göran Andersson" <gu***@guffa.co m> wrote in message
news:%2******** ********@TK2MSF TNGP05.phx.gbl. ..
> Daniel wrote:
>> My previous thread got very large so here is my point again, but a
>> better example of my problem:
>>
>> SceneChair chair =
>> (SceneChair)_ob jMan.GetObject( (int)ObjectID.S eats);
>>
>> chair.Position = GetSeatPosition (1);
>>
>> chair.FrameNum = 1;
>>
>> _objMan.AddToRe nderStack(chair );
>>
>> SceneChair chair2 = new SceneChair();
>>
>> chair2 = chair;
>>
>> chair2.Position = GetSeatPosition (3);
>>
>> chair2.FrameNum = 2;
>>
>> _objMan.AddToRe nderStack(chair 2);
>>
>>
>>
>> Ok in the above code when i debug, at the line chair2.Position both
>> instances are equal in data as you would expect.
> Not only in data. They are the same instance. You are not copying
> the data of the object, but the reference to the object.
>
>> Now when i go to the final _objMan.AddToRe nderStack line both the
>> chair
>> instance and the chair2 instance framenumbers become 2. But i only
>> changed
>> chair2's one so chair being a differenct instance shouldnt be
>> effected
>> right?
> No, it isn't a different instance.
>
>> The SceneChair chair2 = new SceneChair(); should have created a
>> new memory
>> space
> Yes, it does. But you overwrite the reference to that new object,
> so it's thrown away and never used.
>
>> and then when i changed chair2's framenum the memory allocated for
>> chair2 shoudl change while chair's remains the same. Am i right?
>> So why do
>> they both change later on???
> Not later on. As there is only one instance, it changes instantly
> when you change it.
>
>> This is why when i add it to my stack they are
>> equal, as it is changing them as i said before while on the stack
>> as if it
>> were the same instance.
> That is because they *are* the same instance.

May 1 '06 #15
System.Object is something very well defined in CLR, my point was just to
help you keep that in mind.
My name is Lebesgue, not Lebasque, dear Danpil.

"Daniel" <Da*****@vestry online.com> wrote in message
news:Oe******** ******@TK2MSFTN GP04.phx.gbl...
It was an example Lebasque, presume object was something else. Its the
concept i am after.

Cheers Goran that's solved my problem.

I come from a c++ background where pointers and references made it clear
when you were using ref and not. Hence my confusion. Got it now, thanks
for all your help guys.

"Lebesgue" <le******@gmail .com> wrote in message
news:OO******** ******@TK2MSFTN GP04.phx.gbl...
No, it is not right.
C# doesn't have late binding (did you come from VB?) and Object doesn't
have any member called x or y, so this would not work.

//Please note that if x and y are reference types, you will want to clone
them as well.

public object Clone()
{
myObj instance = new myObj();
instance.x = this.x;

return instance;
}

myObj.x
Would the above return a clone not a reference?


it will definitely return a reference to a new object (to the clone). I'm
afraid you do not fully understand what an object and what a reference
is...

"Daniel" <Da*****@vestry online.com> wrote in message
news:ef******** ******@TK2MSFTN GP02.phx.gbl...
But now i am not sure how to create a new instance to do that with, i
always thought 'new' did this.

Is this right?

Clone()
{
Object myObj = new myObj();

myObj.x = this.x;
myObj.y = this.y;

return myObj;
}

Would the above return a clone not a reference?
"Göran Andersson" <gu***@guffa.co m> wrote in message
news:uN******** ******@TK2MSFTN GP03.phx.gbl...
Daniel wrote:
> Thats the answer i was looking for.
>
> I made that bit of code as a basic example buti think that is what is
> happening anyway.
>
> So in that case how can i take an instance in an array and create a
> new instance with the same data?
>
> I want to do this:
>
> Object newInstance = _myList[1];
>
> Where newInstance when changed does not affect _myList[1] after.

As Lebesgue said, you should clone the object. Basically that means
creating a new instance and copy all the data to it.

> I always thought by using the new operator it solved this by
> allocating new memory i didnt realise that the '=' sign copied the
> reference and not the actual data.
>
> Could you show me how to do this? What i dont understand is i do this
> kind of thing all the time, the only difference here is that i am
> using List instead of array, i preumse arrays create new instances and
> lists use the reference?

No. Instances are never created automatically. You must have been lucky
in the past. That, or you have a lot of potential bugs lurking in your
code...

> "Göran Andersson" <gu***@guffa.co m> wrote in message
> news:%2******** ********@TK2MSF TNGP05.phx.gbl. ..
>> Daniel wrote:
>>> My previous thread got very large so here is my point again, but a
>>> better example of my problem:
>>>
>>> SceneChair chair =
>>> (SceneChair)_ob jMan.GetObject( (int)ObjectID.S eats);
>>>
>>> chair.Position = GetSeatPosition (1);
>>>
>>> chair.FrameNum = 1;
>>>
>>> _objMan.AddToRe nderStack(chair );
>>>
>>> SceneChair chair2 = new SceneChair();
>>>
>>> chair2 = chair;
>>>
>>> chair2.Position = GetSeatPosition (3);
>>>
>>> chair2.FrameNum = 2;
>>>
>>> _objMan.AddToRe nderStack(chair 2);
>>>
>>>
>>>
>>> Ok in the above code when i debug, at the line chair2.Position both
>>> instances are equal in data as you would expect.
>> Not only in data. They are the same instance. You are not copying the
>> data of the object, but the reference to the object.
>>
>>> Now when i go to the final _objMan.AddToRe nderStack line both the
>>> chair
>>> instance and the chair2 instance framenumbers become 2. But i only
>>> changed
>>> chair2's one so chair being a differenct instance shouldnt be
>>> effected
>>> right?
>> No, it isn't a different instance.
>>
>>> The SceneChair chair2 = new SceneChair(); should have created a new
>>> memory
>>> space
>> Yes, it does. But you overwrite the reference to that new object, so
>> it's thrown away and never used.
>>
>>> and then when i changed chair2's framenum the memory allocated for
>>> chair2 shoudl change while chair's remains the same. Am i right? So
>>> why do
>>> they both change later on???
>> Not later on. As there is only one instance, it changes instantly
>> when you change it.
>>
>>> This is why when i add it to my stack they are
>>> equal, as it is changing them as i said before while on the stack as
>>> if it
>>> were the same instance.
>> That is because they *are* the same instance.
>



May 1 '06 #16
"Daniel" <Da*****@vestry online.com> wrote in message
news:Or******** ********@TK2MSF TNGP05.phx.gbl. ..
<snip>
I always thought by using the new operator it solved this by
allocating new memory i didnt realise that the '=' sign copied the
reference and not the actual data.
// chair is a reference to the thingy in the collection
// It is not a clone as you wish
SceneChair chair = (SceneChair)_ob jMan.GetObject( (int)ObjectID.S eats);

// This modifies the thingy in the original collection
chair.Position = GetSeatPosition (1);
chair.FrameNum = 1;

// now the original thing is referenced by two collections
_objMan.AddToRe nderStack(chair );
// TADA: you have created a brand new virgin Thingy
SceneChair chair2 = new SceneChair();

// WOOPS: You lost the reference to the brand new virgin thingy
// Instead you are pointing to the original thingy living on both
collections
chair2 = chair;

//WOOPS: modify the original thingy again
//and put another reference to it in the second collection
// That is one popular thingy
chair2.Position = GetSeatPosition (3);
chair2.FrameNum = 2;
_objMan.AddToRe nderStack(chair 2);
---------------------------------------------
Could you show me how to do this? Other folks have shown you how to clone the thingy.
So you code should look like this

Thingy myThingy = Manager.GetClon eofThingy(thing yID);
myThingy.Change stuff();
Manager.AddToRe nderStack(myThi ngy );

Thingy thingy2 = myThingy.Clone( )
thingy2 .Changestuff();
Manager.AddToRe nderStack(myThi ngy );

----
Instead of
Thingy thingy2 = myThingy.Clone( )
you could do this
Thingy thingy2 = new Thingy(myThingy ); // copy constructor - calls Clone
internally

What i dont understand is i do this kind of thing all the time, the
only difference here is that i am using List instead of array, i
preumse arrays create new instances and lists use the reference?


Nope, they are the same.

Try this

using System;
using System.Collecti ons;

public class Foo
{
private int x;
public Foo(int x)
{
this.x = x;
}
public int X {get{return x;} set{x = value;}}

public static void Main(string[] args)
{
Foo myFoo = new Foo(0);
Foo[] array = new Foo[5];
for(int i = 0; i < array.Length; i++)
{
myFoo.X++;
array[i] = myFoo;
}
foreach(Foo foo in array)
Console.WriteLi ne(foo.X);
myFoo.X = 19;
foreach(Foo foo in array)
Console.WriteLi ne(foo.X);
}
}

See if the output matches your expectations

Hope this helps
Bill

May 2 '06 #17
"Daniel" <Da*****@vestry online.com> wrote in message
news:Oe******** ******@TK2MSFTN GP04.phx.gbl...
It was an example Lebasque, presume object was something else. Its the
concept i am after.

Cheers Goran that's solved my problem.

I come from a c++ background where pointers and references made it
clear when you were using ref and not. Hence my confusion. Got it now,
thanks for all your help guys.


Just to be clear
The C++ equivalent to your code is this

// chair2 and chair are both pointers to a SceneChair
SceneChair* chair2 = new SceneChair();
chair2 = chair;

And that code is a memory leak

now if you had

SceneChair chair;
SceneChair chair2 = chair; // now we are copying

Bill
May 2 '06 #18
"Lebesgue" <le******@gmail .com> ha scritto nel messaggio
news:OO******** ******@TK2MSFTN GP04.phx.gbl...

//Please note that if x and y are reference types, you will want to clone
them as well.


You *may* want to clone them.
This is the problem of shallow vs deep copy.

I think that if you need massive cloning you made mistakes in your code
design.

Maybe the solution is just the use of a structure (value type: always
copied) instead of a class (reference type: never copied and need of manuale
cloning).
--

Free .Net Reporting Tool - http://www.neodatatype.net
May 2 '06 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3853
by: tommydkat | last post by:
Well, I've finally gotten UDB 8.2 FixPak 3 up and running on my HP-UX 11i system, thanks to Mr McBride and IBM support. :) I created a 32-bit instance and that's running just fine. However, I learned today that I need to have a 64-bit instance. So, can I create a 64-bit instance "next to" the 32-bit instance I've got running now or must I nuke my 32-bit instance and then create a 64-bit instance? Thanks in advance for your help!
4
3149
by: Mike | last post by:
Class A public objX I want to create 2 or more instances of Class A and have the same value for objX in all instances. Instance1 of Class A Instance2 of Class A Instance3 of Class A
8
12206
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
I'm trying to get a list of SQL Server Instances thru a VB.NET application. I have tried GetDataSource and SMO. I have also tried using ListSQLSvr.exe from http://www.sqldev.net/misc/ListSQLSvr.htm I run all of them on a Windows XP Professional 64bit machine which has 2 instances: mymachine\SQLExpress (Express Edition) and MSSQLSERVER (Developer Edition (64bit)) SQL Browser Service: not running
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10536
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10304
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10285
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9114
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2966
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.