Re: Assigning a reference to a variable
- From: Jeff Louie <anonymous@xxxxxxxxxx>
- Date: Thu, 29 Nov 2007 22:09:07 -0800
Jon.. In C++ if *p is a very large object then passing p may avoid a
call to the copy constructor. In C++ classes use value semantics by
default, but in C# classes use reference semantics. So in C# you can
simply return a reference to an object and not worry about a call to the
copy constructor.
We can demonstrate this difference in C++/CLI since C++/CLI supports
both value semantics and reference semantics, unlike C#. So in C++/CLI
if you have ref class SomeClass you can create an object using reference
semantics as in:
SomeClass^ someClass= gcnew SomeClass(); or you can use value semantics
as in:
SomeClass someClass();
SomeClass^ someClass1= someClass does not call the copy constructor.
SomeClass someClass2= someClass does call the copy constructor, but only
in C++/CLI. I don't know of any equivalent in C#.
Another reason to use indirection is to provide read only access to a
variable. This is possible in C++ using the const key word, but const is
not supported in C#. To proved read only access to a read/write
variable, in C# you can wrap a reference to the read/write class in a
wrapper class that simply provides pass through get only access to the
variable using delegation.
Regards,
Jeff
*** Sent via Developersdex http://www.developersdex.com ***
.
- Prev by Date: Static Variables.
- Next by Date: how to get the handle of a child object
- Previous by thread: Re: Assigning a reference to a variable
- Next by thread: About Factory Method and Abstract Factory (design pattern)
- Index(es):
Relevant Pages
|