VS 2005 C++ property

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

For the following simple property:

property int x;

I understand that a hidden backing variable will be created (I like that).
However, why is a simple property any different than a public data member?

I could see the usefulness, if it were possible (for example) to have
private set access and public get accessors, but doesn't this require a
non-simple property?
 
Hi Greg!
property int x;

I understand that a hidden backing variable will be created (I like that).
However, why is a simple property any different than a public data member?

Good question... there might be several reasons to use this, one is:

<quote>
properties are useful when you have a situation where you might need to
customize a field's accessor functions in future, but at present you
have no specific customization to do. Using trivial properties
eliminates the need to use a public field for now and having to later
convert it to a property - this could cause problems as it changes the
very signature of the class - for example reflection treats fields and
properties separately.
</quote>

See Nish´s Article:
http://www.codeproject.com/managedcpp/CppCliProperties.asp

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
Hello Jochen,

With a simple property can (for example) the get accessor be defined, and
leave the set automatic as is? I would think not since once a get is defined
it must reference a variable and the hidden backing variable is probably no
longer created so that the set must also either be defined or not used (i.e.
get only). Then truly all that simple properties do is create a signature
placeholder? In that case they don't provide what I hoped - automatic backing
variables combined with the ability to independently control get and set
access (e.g. one public and one private).
 
Hi Greg!
In that case they don't provide what I hoped - automatic backing
variables combined with the ability to independently control get and set
access (e.g. one public and one private).

If you want control set/get, then you need to do it "by hand". If you
use "property" you can´t control get/set.

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
If you want control set/get, then you need to do it "by hand". If you
use "property" you can´t control get/set.
You can still use "property" and take advantage of the set function being
called via
x = 5; // (for example)

However, I wanted to know if you still leverage the automatic backing
variable which if you can't, doesn't provide what I hoped.
 
Hi Greg!
You can still use "property" and take advantage of the set function being
called via
x = 5; // (for example)

However, I wanted to know if you still leverage the automatic backing
variable which if you can't, doesn't provide what I hoped.

You can only use the property... AFAIK there is no way to access the
backing variable... but I am not sure...

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
Greg said:
However, I wanted to know if you still leverage the automatic backing
variable which if you can't, doesn't provide what I hoped.

The automatic backing variable only exists for trivial properties. If
you have a getter function, you have to hook it up to your own variable.
It's not hard to add one more line of code for that variable.

Tom
 
Back
Top