ユーザー定義型 
- ユーザー定義型
- 概要
- メンバ公開修飾子 - public private(class,struct)
- オーバーライドチェック - override(class)
- 不変条件 - invariant(class,pod,struct)
- 委譲 - implement(class)
- コンストラクタ - this() (class,pod,struct)
- デストラクタ - ~this() (class,struct)
- 静的コンストラクタ - static this() (class,struct,pod,utility)
- 静的デストラクタ - static ~this() (class,struct,pod,utility)
- 静的メンバ関数 (class,interface,pod,struct,utility)
- 静的メンバ変数 (class,pod,struct)
- メンバ関数 (class,interface,struct,pod)
- 変数の属性 - const,ref,in,out
- メンバ変数 (class,struct,pod)
概要 
項目 | pod | struct | class | interface | utility | デフォルトコンストラクタのユーザー定義 | x | x | o | x | x | コンストラクタ | o | o | o | x | x | デストラクタ | x | o | o | x | x | 静的コンストラクタ 静的デストラクタ | o | o | o | x | o | メンバ変数 | o | o | o | x | x | structメンバ変数 | x | o | o | x | x | objectメンバ変数 | x | o | o | x | x | メンバ関数 | o | o | o | o | x | ガベージコレクタ対象 | x | x | o | o | x | インターフェースの継承 | x | x | o | o | x | 公開修飾子(public/private) | o | o | o | x | o | 代入オーバーロード | x | o | x | x | x | invariant | o | o | o | x | x | ver2.0 | scope属性 | x | x | o | x | x | inlineメンバ変数 | o | o | o | x | x |
1 : 全部0で初期化
interface 
- C#,Dのinterfaceと同じ
- interfaceは複数にinterfaceを継承することができる
- 純粋仮装関数にはabstractキーワードを先頭につける
- <要検討>デフォルト実装を書くことを許す(本当は許さない方が厳密なinterfaceなんですけど)
1
2
3
4
5
6
7
|
-
|
|
|
|
!
| interface IButtonListener
{
abstract void onButtonPush( in IButton sender )const;
void onButtonFocused( in IButton sender )const{}
};
|
class 
- 強制的にobject型を継承している
- 他の言語でよくあるclassの継承は不可
- interfaceは何個でも継承できる
pod 
- plain object data
- C言語のstructに近い
- デフォルトコンストラクタはオブジェクトのメモリ領域を0で初期化する扱いになる
struct 
- 継承のないC++のクラス・D言語のscope classと同じ
- ガベージコレクト対象ではないので参照を保持することができない
utility 
- static関数のみ提供する型,インスタンスを作ることができない。
メンバ公開修飾子 - public private(class,struct) 
- protectedは無い。
- デフォルトはpublic
- コロンを使うC++スタイル、メンバ変数・関数に直接記述するJAVAスタイルの両方に対応
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
-
|
|
|
|
|
|
|
|
|
|
|
!
| class A
{
public:
void funcPubA(){} private:
void funcPriA(){}
public void funcPubB(){} private void funcPriB(){}
};
|
オーバーライドチェック - override(class) 
不変条件 - invariant(class,pod,struct) 
- (all)Dのinvariantとほぼ同じ
- 呼び出されるタイミング
- publicなメンバ関数の実行直前・直後
- コンストラクタの直後
- デストラクタの直前
- invariant関数内でpure constメンバ関数以外のメンバ関数を呼び出すことを禁止。
- <要検討>structで再帰呼び出しされたらどうしよう(classはカウンタ持たせればいいかな)
委譲 - implement(class) 
- interfaceのデフォルト実装を別オブジェクトに委譲するキーワード
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
-
|
|
!
-
|
|
!
-
|
|
|
|
|
|
!
-
|
|
|
|
!
| interface IButtonListener
{
abstract void onPushed();
void onFocused(){}
};
class Hoge : IButtonListener
{
public:
override void onPushed(){}
};
class A : IButtonListener implement mHoge
{
private:
Hoge mHoge = new Hoge();
};
class B : public IButtonListener implement mHoge
{
override void onFocused() { }
private:
Hoge mHoge = new Hoge();
};
|
コンストラクタ - this() (class,pod,struct) 
- (all)D言語の形式を採用し、関数名はthisを使う
- (all)別コンストラクタ関数コールは可能
- (all)どのコンストラクタよりも前に.Initデータをメモリ領域にセットされる(D言語と同じふるまいをする)
- (pod)デフォルトコンストラクタは定義できない(0初期化固定)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
-
|
|
-
|
!
|
-
|
|
|
!
|
|
|
!
| class A
{
public:
this()
{
mY = mX + 1;
}
this(in int x, in int y)
{
this();
mX = x;
mY = y;
}
int x = 1; int y; };
|
デストラクタ - ~this() (class,struct) 
0
1
2
3
4
5
| class A
{
public:
~this()
{
}
};
|
静的コンストラクタ - static this() (class,struct,pod,utility) 
- D言語と同じ
- 静的コンストラクタが呼ばれる前に,.initデータがメモリ領域にコピーされる。
0
1
2
3
4
5
6
7
8
9
10
11
| class A
{
static int hoge = 1;
static int foo; // hoge * 2を代入したいが,他の変数を参照することができない
static this()
{
// この時点で,.initデータがコピー済み。
// つまり,hogeには1,fooには0(デフォルト値)が代入済み
foo = hoge * 2; // ここなら他の変数を参照することができる。
}
};
|
静的デストラクタ - static ~this() (class,struct,pod,utility) 
0
1
2
3
4
| class A
{
static ~this()
{
}
};
|
静的メンバ関数 (class,interface,pod,struct,utility) 
静的メンバ変数 (class,pod,struct) 
メンバ関数 (class,interface,struct,pod) 
- (all)C++でいうメンバ関数のconst修飾子はあり。引数リストの括弧の後ろにconstを付ける。
- (class)抽象関数にoverrideキーワードが使える
- (interface)純粋仮装関数には先頭にabstractキーワードを付ける
- (interface)デフォルト実装はstatic関数として同じ名前の関数が定義される。
1
2
3
4
5
6
7
|
-
|
|
|
|
!
| interface IName
{
string getName(){ return "NoName"; }
};
|
変数の属性 - const,ref,in,out 
const
- const属性のメンバ関数しか呼ぶことができない。(変数を変更することができない)
1
2
3
4
5
6
7
8
9
10
11
12
13
|
-
|
|
|
|
-
|
|
|
!
!
| pod Hoge
{
public int a()const { return 1; }
public int b() { return 2; }
static void test()
{
const Hoge hoge;
hoge.a(); hoge.b(); }
};
|
ref
- 参照型。Cでいうポインタ。
- 参照は保持することはできない。
- 関数の引数および戻り値でのみ使用可能。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
-
|
-
|
!
|
|
-
|
|
!
!
| pod Hoge
{
static public void a( ref int b )
{
b = 2;
}
static void test()
{
int hoge = 0;
Hoge.a( hoge ); }
};
|
in
out
メンバ変数 (class,struct,pod) 
publicなメンバ変数は関数に展開される。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
!
| class Hoge
{
public Foo a;
}
|
inlineメンバ変数 
1
2
3
4
5
6
7
8
9
|
-
|
|
|
|
!
| class Hoge
{
public inline Foo a;
};
|
|