2016-05-22 7 views
-4

以下のコードはC#での例として作成したものですが、C++で行う必要があります行う?クラスプロパティの型をC++で作成する方法は、C#で行われる方法と同じです#

public class MyClassTest{ 
    public int testint1{get;set;} 
    public MyClassTest2 classTest2{get;set;} 
} 
public class MyClassTest2{ 
    public int testint2{get;set;} 
    public MyClassTest classTest{get;set;} 
} 
+3

Javaと同じように、C++には同等の標準はありません。 C++ 17 * shrug *ではライブラリとして可能かもしれませんが。 – chris

答えて

0

このようなものです。

class MyClassTest { 
private: // optional: C++ classes are private by default 
    int testint1; 
public: 
    int getTestInt1() const { return testint1; } 
    void setTestInt1(int t) { testint1 = t; } 
}; 

それとも、あなたのメンバー名が別個にし、取得/設定キーワードをスキップすることができます:

class MyClassTest { 
private: 
    int testint1_; 
public: 
    int testint1() const { return testint1_; } 
    void testint1(int t) { testint1_ = t; } 
}; 
0

現在のC++標準では、このに相当するものはありません、あなただけのgetter/setterメソッドを作成する必要がありますあなたが望む任意のフィールドのための方法:Visual Studioで

class MyClass { 
public: 
    MyClass() {} 
    // note const specifier indicates method guarantees 
    // no changes to class instance and noexcept specifier 
    // tells compiler that this method is no-throw guaranteed 
    int get_x() const noexcept { return x; } 
    void set_x(int _x) { x = _x; } 
private: 
    int x; 
}; 
0

(鉱山は2013年である)、それはこの方法で行うことができます。

クラス内の
__declspec(property(get = Get, put = Set)) bool Switch; 

bool Get() { return m_bSwitch; } 
void Set(bool val) { m_bSwitch = val; } 

bool m_bSwitch; 

です。

関連する問題