"Virtual Properties" feature of Microsoft Visual C
Translator Note:
This paper briefly introduces the method of using the __Declspec keyword in Microsoft Visual C to implement the "property" C . More detailed information about the __DECLSpec keyword, you can refer to MSDN.
__declspec keyword is not part of standard C , so this implementation "Property" method is only available for Visual C , if you want to understand how to implement "Properties" in standard C , please refer to:
http://www.9cbs.net/develop/read_article.asp?id=18361
text:
Many legacy of traditional C code often appear with public or protected keyword modified member variables, you can go directly to access them (the translator's note, "if it is protected, you can directly access it directly in its derived class), and Not through a set of simple GET / SET methods. For example, the following structure definition is this:
TYPEDEF STRUCT Tagmystruct {long m_lvalue1; ... // rest of the structure definition.} SMYSTRUCT DISTRUCT;
In the client program that uses this structure, you can see the code that is scattered as listed below:
SMYSTRUCT MYSTRUCT; Long LtempValue;
MyStruct.m_lvalue1 = 100; // or any other value this is to be assigned to it. ... ltempvalue = mYStruct.m_lvalue1;
In this case, once this code needs to apply in a multi-threaded environment, you will encounter a trouble. Because there is no existence of the Get / SET method, you cannot simply add a critical area (or mutex) to protect all public member variables including M_LValue1 in the definition of smystruct.
If you are using the Microsoft Visual C compiler, you can easily find a solution to solve this problem.
You only need to write your structure as the following form:
typedef struct tagMyStruct {__declspec (property (get = GetValue1, put = PutValue1)) long m_lValue1; ... // Rest of the structure definition long GetValue1 () {// Lock critical section return m_lInternalValue1;. // Unlock critical section. } Void Putvalue1 (long Lvalue) {// Lock critical section m_linternalValue1 = LVALUE; // unlock critical section} private: long m_linternal section} private: long m_linternal section means; // define critical section member line.
This is all you have to do! After this, for the following code:
Mystruct.m_lvalue1 = 100
The compiler will automatically convert to:
MyStruct.putValue (100)
For the following code:
LTEMPVALUE = mystruct.m_lvalue1
The compiler will automatically convert to:
LtempValue = mystruct.getvaluel ()
Such specific features can bring a lot of useful features, you can even use it for your original structure or class with reference to the reference count!
Translator added:
For classes such as arrays, VC also provides corresponding support, as follows:
#include
In VC6 and VC7, there is slightly different from the process of multi-dimensional array, as above
__DECLSPEC (Property (get = getValue1, put = putvalue1)) INT T [] [];
In VC6, you can simply write int Ti []; you can support two-dimensional arrays, and you must be written in VC7.
Originally selected from: http://www.codeproject.com/cpp/virtual_property.asp