也用 C++ 實現 Property 功能 (轉)
用 或者 Borland C++ Builder 的時候會遇見“property”這個東西,可以控制讀寫方式。標準 C++ 並沒有這個概念,那麼我們能否在純 C++ 中使用這個功能呢?當然可以。下面是本人獨立完成的一個實現方法:
#include
using namespace std;
typedef enum {READONLY, READWRITE} RW_Right;
template
RW_Right = READWRITE /* 該屬性的:預設為讀寫 */ >
class Property
{
public:
typedef _Ty* (_Class::*Getter)(void);
typedef void (_Class::*Setter)(const _Ty&);
public:
Property(_Class& _src /* 目標*/,
const Getter _getter /* 取值 */,
const Setter _setter /* 賦值函式 */)
:m_src(_src), m_getter(_getter), m_setter(_setter)
{}
operator _Ty&() /* 返回 lvalue */
{
return *(m_src.*m_getter)(); /* 可以直接對原始資料賦值 */
}
operator const _Ty&() const
{
return *(m_src.*m_getter)();
}
_Ty& operator=(const _Ty& _value) /*返回一個 lvalue */
{
(m_src.*m_setter)(_value);
return *(m_src.*m_getter)();
}
private:
_Class& m_src; /* 儲存宿主類資訊 */
Getter m_getter; /* 宿主取值函式 */
Setter m_setter; /* 宿主賦值函式 */
Property(); /* 必須在宿主類建立的同時初始化 */
Property(const Property&);
Property& operator=(const Property&);
};
template
class Property <_class _ty="" readonly="">
{
public:
typedef _Ty* (_Class::*Getter)(void);
public:
Property(_Class& _src, const Getter _getter)
:m_src(_src), m_getter(_getter)
{}
operator const _Ty() const /* 只能返回 rvalue */
{
return *(m_src.*m_getter)();
}
private:
_Class& m_src;
Getter m_getter;
Property();
Property(const Property&);
Property& operator=(const Property&);
_Ty& operator=(const _Ty& _value); /* 禁止賦值操作 */
};
class MyClass
{
public:
int* get_count()
{
cout << "Getting property" << endl;
return &m_count;
}
void set_count(const int& _count)
{
cout << "Setting property" << endl;
m_count = _count;
}
public:
MyClass()
:Count(*this, &MyClass::get_count, &MyClass::set_count)
{}
Property
private:
int m_count;
};
int main(int argc,char* argv[])
{
MyClass mc;
mc.Count = 2;
int j = mc.Count;
cout << j << endl;
return 0;
}
以上測試編譯會出現 warning
warning C4355: “this” : 用於基成員初始值設定項列表
不用擔心。 7.1 編寫,本人版權所有,有意見建議請發短訊息。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10748419/viewspace-963513/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 在C++中實現“屬性 (Property)” (轉)C++
- 用C++實現下載檔案的功能C++
- Visual C++實現檔案間批次轉換功能 (轉)C++
- 用Descriptor來實現類級屬性(Property)
- 用標準C實現shell功能(轉)
- Visual C++ 6.0程式設計實現列印功能(轉)C++程式設計
- 批次抓圖原來用Word也可實現(轉)
- 用ASP實現論壇的UBB功能 (轉)
- 用VC++實現上網撥號功能 (轉)C++
- property的系統實現分析
- 用CSS實現類似導航翻轉功能例子CSS
- 用pandas實現SQL功能SQL
- VB的ShowInTaskbar功能分析以及用VC的實現 (轉)
- C++:用棧實現反轉連結串列,超簡單!C++
- C/C++實現strcpy和strcat兩個功能C++
- 用原生 JS 實現 innerHTML 功能JSHTML
- 用java實現ftp的功能JavaFTP
- 用Visual C++實現圖象漸顯和漸隱 (轉)C++
- C++完美實現Singleton模式[轉]C++模式
- C/C++ Y/N 重複輸入功能實現C++
- 用c++實現淨現值的計算C++
- 用C++ Builder 實現類似ie位址列的ComboBox (轉)C++UI
- koa框架會用也會寫—(koa的實現)框架
- QT:用QWebSocket實現webchannel,實現C++與HTML通訊QTWebC++HTML
- Linux下VLAN功能的實現 (轉)Linux
- 再探C++的單件實現 (轉)C++
- C++ Qt開發:TabWidget實現多窗體功能C++QT
- 用iptables實現NAT(轉)
- MySQL用表實現號池的功能MySql
- 用C++模板描述的連結串列、棧、佇列(宣告與實現) (轉)C++佇列
- 能用js實現的最終用js實現,Shell指令碼也不例外JS指令碼
- DELPHI也可以實現控制元件陣列,用定義陣列變數實現控制元件陣列 (轉)控制元件陣列變數
- 在C++中實現變長陣列 (轉)C++陣列
- C# 實現將 PDF 轉文字的功能C#
- VB實現SQL Enterprise Manager功能 (轉)SQL
- [C++ & AdaBoost] 傻陳帶你用C++實現AdaBoostC++
- 用低程式碼平臺,5分鐘實現報價轉訂單功能
- 實現Inshot功能的短影片應用