0

私はその出力を尋ねるこの質問に出くわしました。

#include<iostream>
using namespace std;
class A{
      public:
            int i;
            A(int j=3):i(j){}
};
class B:virtual public A{
      public:
            B(int j=2):A(j){}
};
class C:virtual public A{
      public:
            C(int j=1):A(j){}
};
class D:public B, public C {
      public:
            D(int j=0):A(j), B(j+1), C(j+2){}
};
int main()
{
D d;
cout<<d.i;
return 0;
}

わからなかったことがいくつかあります。これらの疑問を明確にしてください。何を検索すればよいかわからなかったため、グーグルできませんでした。

Q1。コードと同様に、パラメーター化されたコンストラクターが使用されます。colon(:)の直後に、親クラスのコンストラクターを記述します。

A(int j=3):i(j){}

使用されている?私はクラスではないので。

Q2。クラスDでは、クラスのコンストラクターが基本クラスの初期化にコンストラクターを使用していますが、すべてのコンストラクターがクラスAの変数iのみを変更していることがわかります。次に、ここで呼び出すコンストラクターのシーケンスは何ですか。

親クラスのコンストラクターを呼び出さない場合は明示的に呼び出され、順序はよく知られていますが、ここのように暗黙的にコンストラクターを呼び出すとどうなりますか。

Q3。パラメーターが初期化されているにもかかわらず、コンストラクターで送信する値が違いを生むようです。なぜそうなのですか?

4

4 に答える 4

4

A1. :i(j) in A(int j=3):i(j){} is an initializer list. Initializer lists can specify how parent classes and member variables are initialized. (j) is the initializer for i and behaves similarly to initialization for a local variable: int i(j);. (you may be more familiar with the initialization syntax int i = j; which is similar. You can't use the = syntax in initializer lists.)

A2. Virtual base classes are always initialized exclusively by the most derived class's constructor. So D's constructor initializes its A base class and when D's constructor calls the constructors for B and C those constructors do not reinitialize A.

A3. The syntax D(int j=0) does not initialize the parameter. Instead it declares a default value for the parameter which is ignored whenever you explicitly pass a value for that parameter.

于 2013-01-04T23:54:21.143 に答える
3

The ctor-initializer-list contains the initializers for all subobjects. That means base class subobjects and member subobjects.

Subobjects are initialized in the order in which they appear in the class definition, always. it doesn't matter what order you put them in the ctor-initializer-list (although putting them in any other order is confusing when the order is ignored).

Inherited base subobject constructors are called by the constructor of the class which is directly derived... except for virtual base subobjects, which are called directly from the constructor for the most-derived object. Virtual base subobjects are constructed before anything else.

There's no such thing as "parameters being initialized". Those are default arguments, and they are ignored when an actual argument is provided.

于 2013-01-04T23:27:02.317 に答える
0

1) The colon : can be used in a constructor to call the constructors of member variables with the given arguments:

public:
        int i;
        A(int j=3):i(j){}

means that A's member i will call its constructor with j as the argument.

2) Subobjects will be initialized in the order they appear in the class definition

class D:public B, public C

3) They weren't initialized, they were given default arguments.

于 2013-01-04T23:33:56.503 に答える
0

Q1: That code in the constructor between the signature of the function and before the opening bracket of the function body is called an initializer list.

The syntax of an initializer list is a listing of member variables of the class with corresponding initial values. The initial values are in parenthesis. The member variables are separate by comma's. The first variable comes after a colon.

An initializer list can also include a constructor to base class, which is what your B and C class do. Your 'A' class simply uses the form that initializes a member variable.

Q2: To find the order that the constructors are executed in, put in some print statements. It will help you understand the order of construction. Also put in a destructor and put print statements in those too. I can't do all of your homework for you on this question. :)

Q3: I guess by parameters being initialized you mean the default arguments? Default arguments are always always overridden if a value is actually passed into the function.

于 2013-01-04T23:37:37.240 に答える