C++におけるタイプ識別

3401 ワード

  • オブジェクトに向かって次のような状況が発生する可能性があります.
  • ベースクラスポインタは、サブクラスオブジェクト
  • を指す.
  • ベースクラス参照は、サブクラスオブジェクトの別名
  • となる.
    //Base* Base&     
    //Derived     
    Base* p = new Derived();
    Base& r = *p;
    

    タイプの違い:
  • 静的タイプ
  • 変数(オブジェクト)自身のタイプ
  • ダイナミックタイプ
  • ポインタ(参照)が指すオブジェクトの実際のタイプ
  • void test(Base* b)
    {
        //       
        Derived* d = static_cast(b);
    }
    

    上記のコードでは、ベースクラスポインタが強制的にタイプをサブクラスポインタに変換できるかどうかは、ダイナミックタイプに依存します.
    C++ではマルチステートを利用してダイナミックタイプを得る
  • ベースクラスで定義された虚関数は、特定のタイプ情報
  • を返す.
  • すべての派生クラスは、タイプに関連する虚関数
  • を実装する必要があります.
  • 各クラスのタイプの虚関数は、異なる実装
  • を必要とする.
    例を挙げます.
    #include 
    #include 
    
    using namespace std;
    
    //    
    class Base
    {
    public:
        //       
        virtual string type()
        {
            return "Base";
        }
    };
    
    //         
    class Derived : public Base
    {
    public:
        string type()
        {
            return "Derived";
        }
        
        void printf()
        {
            cout << "I'm a Derived." << endl;
        }
    };
    
    //         
    class Child : public Base
    {
    public:
        string type()
        {
            return "Child";
        }
    };
    
    void test(Base* b)
    {
        /*         */
        // Derived* d = static_cast(b);
        
        //                      ,     
        if( b->type() == "Derived" )
        {
            Derived* d = static_cast(b);
            
            d->printf();
        }
        
        // cout << dynamic_cast(b) << endl;
    }
    
    
    int main(int argc, char *argv[])
    {
        Base b;
        Derived d;
        Child c;
        
        test(&b);
        test(&d);
        test(&c);
        
        return 0;
    }
    

    実行結果:
    I'm a Derived.
    

    上記のソリューションにはいくつかの欠陥があります.
  • は、ベースクラスからタイプ虚関数
  • を提供する必要がある.
  • すべての派生クラスは、タイプ虚関数
  • を書き換える必要があります.
  • 各派生クラスのタイプ名は、
  • のみでなければなりません.
    では、これらの欠点を改善できる別の案はありますか?はい、ここでは新しい知識点を学びましょう.
  • C++はタイプ情報を取得するためのtypeidキーワードを提供する
  • typeidキーワードは、対応するパラメータのタイプ情報
  • を返す.
  • typeidはtypeを返します.infoクラスオブジェクト
  • typeidのパラメータがNULLの場合異常
  • が放出される.
  • typeidキーワードの使用
  • int i = 0;
    
    const type_info& tiv = typeid(i);
    const type_info& tii = typeid(int);
    
    cout << (tiv == tii) << endl;
    
  • typeidの注意事項
  • パラメータがタイプである場合:静的タイプ情報
  • を返す.
  • パラメータが変数の場合:
  • 虚関数テーブルが存在しない---静的タイプ情報を返す
  • 虚関数テーブルが存在する---動的タイプ情報
  • を返す


    使用例:
    #include 
    #include 
    #include 
    
    using namespace std;
    
    //    
    class Base
    {
    public:
        //        
        virtual ~Base()
        {
        }
    };
    
    class Derived : public Base
    {
    public:
        void printf()
        {
            cout << "I'm a Derived." << endl;
        }
    };
    
    void test(Base* b)
    {
        const type_info& tb = typeid(*b);
        
        //tb type_info ,  name            
        cout << tb.name() << endl;
    }
    
    int main(int argc, char *argv[])
    {
        int i = 0;
        
        //    typeid     
        const type_info& tiv = typeid(i);
        const type_info& tii = typeid(int);
        
        cout << (tiv == tii) << endl;
        
        Base b;
        Derived d;
        
        test(&b);
        test(&d);
        
        return 0;
    }
    

    出力結果:
    1
    4Base
    7Derived