首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> VC >> 關於VC++

標準 C++ 實現 C# 風格 Property (屬性) 模板類別:operator= 與型別轉換運算子封裝 Get/Set

     編輯:關於VC++

動機

C# property 提供 get/set 存取器,語法如欄位般直觀,且可控制唯讀/唯寫/讀寫。標準 C++ 無原生支援,本文以 純標準 C++ 模板 實現零擴展、可攜帶的 property<> 類別。

設計目標

  • 語法:obj.Prop = val; (呼叫 set) / val = obj.Prop; (呼叫 get)
  • 支援 READ_ONLY、WRITE_ONLY、READ_WRITE 三種模式
  • 編譯期/執行期斷言保護非法存取
  • 零額外儲存開銷 (僅儲存函數指標與容器指標)

核心實作


#define READ_ONLY  1
#define WRITE_ONLY 2
#define READ_WRITE 3

template 
class property {
public:
    property() : m_pContainer(nullptr), m_pSet(nullptr), m_pGet(nullptr) {}

    // 綁定容器實例 (通常在容器建構函數中呼叫)
    void setContainer(Container* pObj) { m_pContainer = pObj; }

    // 綁定 set 成員函數指標
    void setter(void (Container::*pFunc)(ValueType)) {
        if (nPropType == WRITE_ONLY || nPropType == READ_WRITE) m_pSet = pFunc;
        else m_pSet = nullptr;
    }

    // 綁定 get 成員函數指標
    void getter(ValueType (Container::*pFunc)()) {
        if (nPropType == READ_ONLY || nPropType == READ_WRITE) m_pGet = pFunc;
        else m_pGet = nullptr;
    }

    // 指派運算子 → 呼叫 set
    ValueType operator=(const ValueType& val) {
        assert(m_pContainer && m_pSet);
        (m_pContainer->*m_pSet)(val);
        return val;
    }

    // 型別轉換運算子 → 呼叫 get
    operator ValueType() {
        assert(m_pContainer && m_pGet);
        return (m_pContainer->*m_pGet)();
    }

private:
    Container* m_pContainer;
    void (Container::*m_pSet)(ValueType);
    ValueType (Container::*m_pGet)();
};

使用範例


class PropTest {
public:
    PropTest() {
        Count.setContainer(this);
        Count.setter(&PropTest::SetCount);
        Count.getter(&PropTest::GetCount);
    }

    int GetCount() const { return m_nCount; }
    void SetCount(int v) { m_nCount = v; }

    // 可讀寫 int 屬性
    property Count;

private:
    int m_nCount = 0;
};

// 使用
PropTest test;
test.Count = 5;      // 呼叫 SetCount(5)
int n = test.Count;  // 呼叫 GetCount() → 5

唯讀/唯寫示範


// 唯讀屬性
property ReadOnlyProp;
// test.ReadOnlyProp = 10; // 執行期 assert 失敗
int v = test.ReadOnlyProp; // OK

// 唯寫屬性
property WriteOnlyProp;
test.WriteOnlyProp = 20; // OK
// int v = test.WriteOnlyProp; // 執行期 assert 失敗

優缺點分析

優點缺點
語法直觀,符合 C#/VB 習慣每個屬性需一個 property 實例,記憶體微量增加
編譯期型別安全,執行期模式檢查需在建構函數手動綁定 set/get/container
純標準 C++,跨平台 (GCC/Clang/MSVC)無法像 C# 直接在類別內宣告 (需分離綁定步驟)
可擴展支援通知事件、驗證邏輯直接呼叫 Get/Set 函數效能略高 (無運算子解析)

原始碼下載

完整標頭檔 property.h、測試主程式、CMakeLists.txt (跨平台建置驗證)。

本文配套源碼

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved