CLI封装类时,涉及确定性析构与非确定性析构,属性封装使用property。
【1】C++导出类
class EXPORTDLL_CLASS CAddSub { public: CAddSub(){ m_len = 0; } ~CAddSub(){ } public: int Add(int x, int y){ return x+y; } int Sub(int x, int y){ return x-y; } int GetLength() { return(m_len); } void SetLength(int len){ m_len = len; } private: int m_len; };
【2】CLI类封装
/// <summary> /// 4 类测试 /// </summary> public ref class AddSubCls { public: AddSubCls() { m_pCls = new CAddSub(); } !AddSubCls() //确定性释放 { delete m_pCls; } ~AddSubCls() //非确定性释放 { this->!AddSubCls(); } public: Int32 Add(Int32 x, Int32 y) { return(m_pCls->Add(x, y)); } Int32 Sub(Int32 x, Int32 y) { return(m_pCls->Sub(x, y)); } property Int32 Length { Int32 get() { return(m_pCls->GetLength()); } void set(Int32 len) { m_pCls->SetLength(len); } } private: CAddSub *m_pCls; };
C#测试函数:
AddSubCls addSubCls = new AddSubCls(); int sum = addSubCls.Add(1, 4); addSubCls.Length = 6;
时间: 2024-12-27 23:26:43