class problems

  • Thread starter Thread starter Timothy V
  • Start date Start date
T

Timothy V

Hi,
I'm having trouble figuring out something. How do i pass a class instance to
a function without 'creating a copy'. In C/C++ i would use &. For example
(C/C++):
class CLASS;
void function(CLASS &c);

Would someone be able to explain this?

Thank you very much in advance,

Timothy.
 
IIRC this passes the ref of the existing instance?
You want to pass by ref in this case which will pass the actual reference
instead of a new reference pointing to the same object.

To clarify
void FunctionX(MyCustomClass MyCustomInstance)
Calls to FunctionX will create a new ref on the stack pointing to
MyCustomInstance.
This does not create a new instance of MyCustomClass, rather a new pointer
to the existing instance.

In this case if we set MyCustomInstance to null, the callee will still have
a valid ref to the instance.

If we change it to
void FunctionX(MyCustomClass ref MyCustomInstance)
Then calls to FunctionX will pass the existing ref on the stack and not
create a new one.

In this case if we set MyCustomInstance to null, the callee will not have a
valid ref to the instance any more.

Have a read of Jon Skeet's site, he has an excellent FAQ re this sort of
thing.
Address is
http://www.pobox.com/~skeet


Cheers
JB
 
Back
Top