A lot of legacy C++ code exists wherein member variables are directly exposed using the public or protected keywords instead of simple accessor / mutator functions. For example consider the following simple structure definition
typedef struct tagMyStruct
{
long m_lValue1;
...
} SMyStruct;
Scattered throughout the client code that uses this structure will be code like the following:
SMyStruct MyStruct;
long lTempValue;
MyStruct.m_lValue1 = 100;
...
lTempValue = MyStruct.m_lValue1;
Now if the module that has all the above code is suddenly required to be used in a thread safe environment, you run into a problem. Because no accessor or mutator functions exist, you cannot just put a critical section (or mutex) within the definition of SMyStruct and protect m_lValue1 or any of the other public member variables.
If it is guaranteed that you will be using the Microsoft Visual C++ compiler then there is an easy solution at hand.
Just enhance the structure as follows:
typedef struct tagMyStruct
{
__declspec(property(get=GetValue1, put=PutValue1))
long m_lValue1;
...
long GetValue1()
{
return m_lInternalValue1;
}
void PutValue1(long lValue)
{
m_lInternalValue = lValue;
}
private:
long m_lInternalValue1;
} SMyStruct;
That's all.
For code such as
MyStruct.m_lValue1 = 100
the compiler will automatically substitute
MyStruct.PutValue1(100)
For code like
lTempValue = MyStruct.m_lValue1
the compiler will substitute
lTempValue = MyStruct.GetValue1()
The possibilities this opens up are many. You can even use this to add reference counting to legacy structures and classes.