T
tshad
I am confused on whether pointers are passed or objects are passed on return
of function where we are returning an object. This is important as I need
to have 2 objects and will use one or the other based on certain criteria.
This is mainly because of the difficulty with copying one object to another.
In my case, I am going to build object1 and return it. Then I build object2
and return that. And if object 2 fails some test, I will use object1.
This is why I can't just use a pointer to return it as I would wipe out
object1 if I were pointing at the same object.
For example:
class A
{
Response object1 = new Response();
Response object2 = new Response();
Response objectPtr = null;
objectPtr = object1;
objectPtr = BuildData();
objectPtr = object2;
objectPtr = BuildData();
}
class B
{
Response BuildData()
{
Response oResponse = new Response();
....
return oResponse;
}
}
class Response
{
stuff
}
In Class A, before I call BuildData()
1) I have an Response object (object1) which is instantiated.
2) I then point to object1 with objectPtr.
3) I then call BuildData() and instantiate another Response object
(oResponse)
4) Before leaving BuildData(), I have 2 distinct objects (object1 and
oResponse).
5) I know that what I really have is 2 pointers (object1 and oResponse)
that point to 2 objects out on the Heap.
6) I now return and pass back oReturn.
What happens here?
A) Does object1 just now point to the object that oResponse was pointing at
and the original object that object1 was pointing at get trashed and garbage
collected since now nothing points at it?
B) Or does the object that oResponse was pointing at get copied to the
object that object1 was pointing at?
C) Something else.
I assume it is "A".
Then when I do the 2nd BuildData(), the same thing happens and object2 will
now point at a new object that the 2nd call to BuildData() created, whereas
the 1st object will just get garbage collected.
If this is the case, I really only have to set up the objects originally as:
Response object1 = null;
Response object2 = null;
Response objectPtr = null;
Since these objects will never be used.
Thanks,
Tom
of function where we are returning an object. This is important as I need
to have 2 objects and will use one or the other based on certain criteria.
This is mainly because of the difficulty with copying one object to another.
In my case, I am going to build object1 and return it. Then I build object2
and return that. And if object 2 fails some test, I will use object1.
This is why I can't just use a pointer to return it as I would wipe out
object1 if I were pointing at the same object.
For example:
class A
{
Response object1 = new Response();
Response object2 = new Response();
Response objectPtr = null;
objectPtr = object1;
objectPtr = BuildData();
objectPtr = object2;
objectPtr = BuildData();
}
class B
{
Response BuildData()
{
Response oResponse = new Response();
....
return oResponse;
}
}
class Response
{
stuff
}
In Class A, before I call BuildData()
1) I have an Response object (object1) which is instantiated.
2) I then point to object1 with objectPtr.
3) I then call BuildData() and instantiate another Response object
(oResponse)
4) Before leaving BuildData(), I have 2 distinct objects (object1 and
oResponse).
5) I know that what I really have is 2 pointers (object1 and oResponse)
that point to 2 objects out on the Heap.
6) I now return and pass back oReturn.
What happens here?
A) Does object1 just now point to the object that oResponse was pointing at
and the original object that object1 was pointing at get trashed and garbage
collected since now nothing points at it?
B) Or does the object that oResponse was pointing at get copied to the
object that object1 was pointing at?
C) Something else.
I assume it is "A".
Then when I do the 2nd BuildData(), the same thing happens and object2 will
now point at a new object that the 2nd call to BuildData() created, whereas
the 1st object will just get garbage collected.
If this is the case, I really only have to set up the objects originally as:
Response object1 = null;
Response object2 = null;
Response objectPtr = null;
Since these objects will never be used.
Thanks,
Tom