ユーザー定義型 Edit

概要 Edit

項目podstructclassinterfaceutility
デフォルトコンストラクタのユーザー定義xxoxx
コンストラクタoooxx
デストラクタxooxx
静的コンストラクタ
静的デストラクタ
oooxo
メンバ変数oooxx
structメンバ変数xooxx
objectメンバ変数xooxx
メンバ関数oooox
ガベージコレクタ対象xxoox
インターフェースの継承xxoox
公開修飾子(public/private)oooxo
代入オーバーロードxoxxx
invariantoooxx
ver2.0
scope属性xxoxx
inlineメンバ変数oooxx

1 : 全部0で初期化

interface Edit

  • 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 Edit

  • 強制的にobject型を継承している
  • 他の言語でよくあるclassの継承は不可
  • interfaceは何個でも継承できる

pod Edit

  • plain object data
  • C言語のstructに近い
  • デフォルトコンストラクタはオブジェクトのメモリ領域を0で初期化する扱いになる

struct Edit

  • 継承のないC++のクラス・D言語のscope classと同じ
  • ガベージコレクト対象ではないので参照を保持することができない

utility Edit

  • static関数のみ提供する型,インスタンスを作ることができない。

メンバ公開修飾子 - public private(class,struct) Edit

  • protectedは無い。
  • デフォルトはpublic
  • コロンを使うC++スタイル、メンバ変数・関数に直接記述するJAVAスタイルの両方に対応
すべてを展開すべてを収束
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 
-
|
|
|
|
|
|
|
|
|
|
|
!
 
class A
{
    //--- 公開修飾子(デフォルトpublic)
    // C++/Dスタイル
public: 
    void funcPubA(){} // public
private:
    void funcPriA(){} // private
 
    // JAVA/C#/Dスタイル
    public void funcPubB(){} // public
    private void funcPriB(){} // private
 
};

オーバーライドチェック - override(class) Edit

  • D,C#と同じ

不変条件 - invariant(class,pod,struct) Edit

  • (all)Dのinvariantとほぼ同じ
    • 呼び出されるタイミング
      • publicなメンバ関数の実行直前・直後
      • ↑ publicメンバ変数によって自動で作成される関数も対象に含む。(メンバ変数の項を参照)
      • コンストラクタの直後
      • デストラクタの直前
    • invariant関数内でpure constメンバ関数以外のメンバ関数を呼び出すことを禁止。
  • <要検討>structで再帰呼び出しされたらどうしよう(classはカウンタ持たせればいいかな)
    • ↑これってどういう意味だっけ;

委譲 (class) Edit

  • 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 = mHoge
{
    // IButtonListenerの関数を全てmHogeに委譲される
    // implementを使うと自動で次のように展開される
    // override void onPushed(){ mHoge.onPushed(); }
    // override void onFocused(){ mHoge.onFocused(); }
private:
    Hoge mHoge;
};
 
class B : public IButtonListener = mHoge
{
    // onFocusedの関数だけ実装、その他はmHogeにまかせる
    override void onFocused() { /* */ }
private:
    Hoge mHoge = new Hoge();
};

コンストラクタ - this() (class,pod,struct) Edit

  • (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 mX = 1; // 直接初期化子を書くことが出来る。ただし,コンパイル時に決まる定数しか書けない。
        int mY; // 指定がないとデフォルトコンストラクタが呼ばれる。
    };

デストラクタ - ~this() (class,struct) Edit

  • D言語スタイルを採用
  0
  1
  2
  3
  4
  5
class A
{
public:
    ~this()
    {
    }
};

静的コンストラクタ - static this() (class,struct,pod,utility) Edit

  • 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) Edit

  • D言語と同じ
  0
  1
  2
  3
  4
class A
{
    static ~this()
    {
    }
};

静的メンバ関数 (class,interface,pod,struct,utility) Edit

  • D言語と同じ。

静的メンバ変数 (class,pod,struct) Edit

  • D言語と同じ。

メンバ関数 (class,interface,struct,pod) Edit

  • (all)C++でいうメンバ関数のconst修飾子はあり。引数リストの括弧の後ろにconstを付ける。
  • (class)抽象関数にoverrideキーワードが使える
  • (interface)純粋仮装関数には先頭にabstractキーワードを付ける
  • (interface)デフォルト実装はstatic関数として同じ名前の関数が定義される。
すべてを展開すべてを収束
  1
  2
  3
  4
  5
  6
  7
 
-
|
|
|
|
!
interface IName
{
  string getName(){ return "NoName"; }
  // こう書くと自動でstatic関数が作られる
  // static string getName() { return "NoName"; }
  // IName.DEFAULT.getName( IName )でアクセスできる
};

変数の属性 - const,ref,in,out,readonly Edit

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(); // OK
        hoge.b(); // const関数ではないので呼ぶことができない。
      }
    };
    

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 ); // hogeは2が入る。
      }
    };
    

in

  • const refのエイリアス。

out

  • refのエイリアス。

readonly

  • C#のものと同じ。
  • コンストラクタでしか変更・代入ができないメンバ変数につける属性。
すべてを展開すべてを収束
  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
 30
 31
 
-
|
|
!
 
 
-
|
|
-
|
|
|
!
|
|
-
|
|
|
|
|
!
|
|
|
|
|
!
 
class Vector2
{
  float x;
  float y;
};
 
class Hoge
{
public:
  this()
  {
    mInt = 2; // コンストラクタからは変更可能
    mVec2 = new Vector2();
    mConstVec2 = new Vector2();
  }
 
  void example()
  {
    mInt = 3; // エラー:コンストラクタではないところから変更できない
    mVec2 = new Vector2(); // エラー
    mVec2.x = 2; // OK
    mConstVec2.x = 2; // エラー
    mVec2.x = mConstVec2.x; // OK
  }
 
private:
  readonly int mA;
  readonly Vector2 mVec2;
  readonly const Vector2 mConstVec2;
};

メンバ変数 (class,struct,pod) Edit

  • publicなメンバ変数は関数に展開される。
  • こうすることで変数のオフセット情報を知る必要がなくなり,再コンパイルの回数を減らすことができる。
    すべてを展開すべてを収束
      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
     30
     31
    
     
    -
    |
    |
    |
    |
    |
    |
    |
    |
    |
    |
    |
    |
    -
    |
    |
    |
    |
    |
    |
    |
    |
    |
    |
    |
    |
    |
    |
    !
    !
    
    struct Hoge
    {
      public int a;
      // これは下記のように展開される
      // public ref int __mv_ref_a()
      // {
      //   return a;
      // }
      // public const ref int __mv_ref_a()const
      // {
      //   return a;
      // }
     
      static void test()
      {
        Hoge h;
        h.a = 3; // h.__mv_ref_a() = 3; に展開される
     
        h.a = Hoge().a + Hoge().a;
        // 展開後
        // {
        //   Hoge __tv00;
        //   Hoge __tv01;
        //   h.__mv_ref_a() = __tv00.__mv_ref_a() + __tv01.__mv_ref_a();
        // }
     
        h.a += 3;
        // 展開後
        // h.__mv_ref_a() += 3;
      }
    }

inlineメンバ変数 Edit

すべてを展開すべてを収束
  1
  2
  3
  4
  5
  6
  7
  8
  9
 
-
|
|
|
|
!
 
 
class Hoge
{
  public inline Foo a;
  // こうするとメンバ変数にダイレクトにアクセスするようになる。
  // invariantメソッドが実装されていても呼ばれなくなる。
  // Fooのメンバ変数の構成が変更されると再コンパイルが必要になる。
};
 

    ホーム 一覧 検索 最終更新 バックアップ リンク元   ヘルプ   最終更新のRSS