-6
class A {
public:
    A (int n = 0) : m_n(n) {}
    A (const A& a) : m_n(a.m_n) {}
private:
    int m_n;
}

また:

namespace std {
    template <class T1, class T2>
    struct pair {
        //type names for the values
        typedef T1 first_type;
        typedef T2 second_type;
        //member
        T1 first;
        T2 second;
        /* default constructor - T1 () and T2 () force initialization for built-in types */
        pair() : first(T1()), second(T2()) {}
        //constructor for two values
        pair(const T1& a, const T2& b) : first(a), second(b) {}
       //copy constructor with implicit conversions
        template<class U, class V>
        pair(const pair<U,V>& p) : first(p.first), second(p.second) {}
    };
    ....
}

コンストラクターがわかりません。これら 2 つのクラスのコンストラクターをコピーします。「:m_n(n)」の部分は何をするのか説明してください。

4

1 に答える 1

1
A (int n = 0) : m_n(n) {}
//           ^^^^^^^^^^

これはmember-initialization-listと呼ばれます。

クラスのコンストラクターの定義では、直接および仮想ベース サブオブジェクトと非静的データ メンバーの初期化子を指定します。

たとえば、次のようになります。

A (int n = 0) : m_n(n) {}

ここで、Aクラスのコンストラクターは で初期化m_nされnます。

ここを見てください。

于 2013-09-15T08:30:43.937 に答える