Microsoft Visual C++的“虛擬屬性”功能
譯者註:
本文簡單介紹了使用Microsoft Visual C++中的__declspec關鍵字來實現“屬性(Property)”這個C++中沒有的特性的方法。有關__declspec關鍵字的更詳細的資訊,可以參考MSDN。
__declspec關鍵字不是標準C++的一部分,因此這種實現“屬性”的方法只適用於Visual C++,如果想要瞭解在標準C++中類比實現“屬性”的方法,請參考:
http://www.csdn.net/develop/read_article.asp?id=18361
本文:
很多遺留下來的傳統C++代碼中常常會出現用public或protected關鍵字修飾的成員變數,您可以直接去訪問它們(譯者注,如果是protected,是指可以在其衍生類別中直接存取),而不是通過一組簡單的get/set方法。舉個例子來說,如下的結構定義就是這樣的情況:
typedef struct tagMyStruct
{
long m_lValue1;
... // Rest of the structure definition.
} SMyStruct;
在使用這個結構體的用戶端程式中就可以看到散布著大量類如下面列出的代碼:
SMyStruct MyStruct;
long lTempValue;
MyStruct.m_lValue1 = 100; // Or any other value that is to be assigned to it.
...
lTempValue = MyStruct.m_lValue1;
在這種情況下,一旦這段代碼需要在一個多線程的環境下應用,你就會遇到一個麻煩。因為沒有get/set方法的存在,你不可能簡單的在SMyStruct的定義中加上一個臨界區(或互斥量)來保護包括m_lValue1在內的所有公有成員變數。
如果您是使用Microsoft Visual C++編譯器,您就可以很方便的找到一個解決這個問題的方案。
您只需把您的結構體重寫為如下的形式:
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_lInternalValue1;
// Define critical section member variable.
} SMyStruct;
這就是您要做的全部!
在這以後,對於如下的代碼:
MyStruct.m_lValue1 = 100
編譯器會自動轉換為:
MyStruct.PutValue(100)
對於如下的代碼:
lTempValue = MyStruct.m_lValue1
編譯器會自動轉換為:
lTempValue = MyStruct.GetValuel()
這樣的特效能帶來很多有用的功能,您甚至可以用它為您原來的結構體或類加上引用計數的功能!
譯者補充:
對於類如數組的情況,VC也提供了相應的支援,如下的例子:
#include <iostream>
using namespace std;
class MyStruct
{
public:
__declspec(property(get=GetValue1, put=PutValue1))
int t[][]; //以二維數組來示範
int GetValue1(int x, int y) //x,y分別對應第一維和第二維的下標
{
return m_lInternalValue1[x][y];
}
void PutValue1(int x,int y, int lValue) //x,y分別對應第一維和第二維的下標,lValue為要賦的值
{
m_lInternalValue1[x][y] = lValue;
}
private:
int m_lInternalValue1[3][3];
};
int main()
{
MyStruct ms;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
ms.t[i][j] = i * j;
return 0;
}
在VC6和VC7中,對於多維陣列的處理略有不同,如上面的
__declspec(property(get=GetValue1, put=PutValue1))
int t[][];
在VC6中可以簡單的寫成int t[];即可支援兩維的數組,而在VC7中必須寫成int t[][];才可以。
原文選自:http://www.codeproject.com/cpp/virtual_property.asp