This is a mirror of official site: http://jasper-net.blogspot.com/

C++ Properties

| Monday, October 18, 2010
Introduction    

This article discusses how to implement C#-like properties in Visual C++ 6 or above. This article assumes that the reader has enough knowledge in the C++ language to understand the code (though the code snippet will be thoroughly explained). It is recommended that the reader understands macros, classes, functions, and typedefs; however, this code is available for anyone to use.  
Background    

If you are a new C++ or C# programmer, then properties may be a new concept. Properties are simply a convenient way of defining a getter and a setter for a given member variable. A getter is a member function that returns the literal value of the member variable that the property represents. The setter sets the literal value of the member variable that the property represents, but does not return a value. Properties in C# typically look like the following:  

private int _x;
public int x
{

get
{
return _x;
}
set
{
_x = value;
}
}  

Of course, get designates the getter and set designates the setter. Within the setter, a variable labeled value exists that represents the rvalue of an assignment statement that involved your property. For example, in the following code snippet, the value variable would contain the value 50.

someObject.x = 50;  

Read more: Codeproject

Posted via email from .NET Info

0 comments: