この文書の現在のバージョンと選択したバージョンの差分を表示します。
cli:property [2009/02/03 20:52] 192.168.0.63 |
cli:property [2013/12/23 09:08] |
||
---|---|---|---|
ライン 1: | ライン 1: | ||
- | {{indexmenu_n>17}} | ||
- | ====== property ====== | ||
- | |||
- | C++で、あるprivateなメンバ変数に対して、読み取り用関数のみ提供したい場合は以下のように書くと思います。 | ||
- | <code cpp> | ||
- | class C { | ||
- | int length; | ||
- | public: | ||
- | C() : length(100) {} | ||
- | int getLength() const { | ||
- | return length; | ||
- | } | ||
- | }; | ||
- | int main() | ||
- | { | ||
- | C c; | ||
- | | ||
- | int len = c.getLength(); | ||
- | return 0; | ||
- | } | ||
- | </code> | ||
- | C++/CLIではプロパティを使って以下のようにかけます。 | ||
- | <code cpp> | ||
- | ref class C { | ||
- | int length; | ||
- | public: | ||
- | C() : length(100) {} | ||
- | property int Length | ||
- | { | ||
- | int get() { return length; } | ||
- | } | ||
- | }; | ||
- | int main() | ||
- | { | ||
- | C c; | ||
- | int len = c.Length; | ||
- | return 0; | ||
- | } | ||
- | </code> | ||
- | 次に値を設定したい場合を考えます。値の設定範囲として0から100までとし、それ以外の値の場合は無視するとします。C++では以下のようになります。 | ||
- | <code cpp> | ||
- | class C { | ||
- | int length; | ||
- | public: | ||
- | C() : length(100) {} | ||
- | int getLength() const { | ||
- | return length; | ||
- | } | ||
- | void setLength(int len) { | ||
- | if ( 0 <= len && len <= 100 ) | ||
- | length = len; | ||
- | } | ||
- | }; | ||
- | int main() | ||
- | { | ||
- | C c; | ||
- | | ||
- | c.setLength(50); | ||
- | return 0; | ||
- | } | ||
- | </code> | ||
- | propertyを使うと以下のようになります。 | ||
- | <code cpp> | ||
- | ref class C { | ||
- | int length; | ||
- | public: | ||
- | C() : length(100) {} | ||
- | property int Length | ||
- | { | ||
- | int get() { return length; } | ||
- | void set(int value) { | ||
- | if ( 0 <= value && value <= 100 ) | ||
- | length = value; | ||
- | } | ||
- | } | ||
- | }; | ||
- | int main() | ||
- | { | ||
- | C c; | ||
- | c.Length = 50; | ||
- | return 0; | ||
- | } | ||
- | </code> | ||
- | |||
- | また上記の例で、宣言と実装を分けて書くと以下のようになります。 | ||
- | <code cpp> | ||
- | ref class C | ||
- | { | ||
- | int length; | ||
- | public: | ||
- | C() : length(100) {} | ||
- | property int Length | ||
- | { | ||
- | int get(); | ||
- | void set(int value); | ||
- | } | ||
- | }; | ||
- | int C::Length::get() | ||
- | { | ||
- | return length; | ||
- | } | ||
- | void C::Length::set(int value) | ||
- | { | ||
- | if ( 0 <= value && value <= 100 ) | ||
- | length = value; | ||
- | } | ||
- | int main() | ||
- | { | ||
- | C c; | ||
- | c.Length = 50; | ||
- | return 0; | ||
- | } | ||
- | </code> | ||
- | |||
- | アクセス指定子をgetとsetで別々に書くこともできます。 | ||
- | <code cpp> | ||
- | public ref class C | ||
- | { | ||
- | int length; | ||
- | public: | ||
- | C() : length(100) {} | ||
- | property int Length | ||
- | { | ||
- | public: | ||
- | int get(); | ||
- | internal: | ||
- | void set(int value); | ||
- | } | ||
- | }; | ||
- | </code> | ||
- | |||
- | |||
- | ===== Trivial Property ===== | ||
- | get()やset()を明示的に書かなくても、コンパイラが勝手に作ってくれます。 | ||
- | <code cpp> | ||
- | public ref class C | ||
- | { | ||
- | public: | ||
- | property int Length; | ||
- | }; | ||
- | </code> |