C++: Construction and initialization order guarant

2019-01-15 08:04发布

I have some doubts about construction and initialization order guarantees in C++. For instance, the following code has four classes X, Y, Z and W. The main function instantiates an object of class X, which contains an object of class Y, and derives from class Z, so both constructors will be called. Additionally, the const char* parameter passed to X's constructor will be implicitly converted to an object of class W, so W's constructor must also be called.

What are the guarantees the C++ standard gives on the order of the calls to the copy constructors? Or, equivalently, what this program is allowed to print?

#include <iostream>

class Z {
   public:
   Z() { std::cout << "Z" << std::endl; }
};

class Y {
   public:
   Y() { std::cout << "Y" << std::endl; }
};

class W {
   public:
   W(const char*) { std::cout << "W" << std::endl; }
};

class X : public Z {
   public:
   X(const W&) { std::cout << "X" << std::endl; }
   private:
   Y y;
};

int main(int, char*[]) {
   X x("x");
   return 0;
}

edit: Is this correct?

   W      |
 /   \    |
Z     Y   |
 \   /    |
   X      V

5条回答
Viruses.
2楼-- · 2019-01-15 08:16

To summarize these are the rules:

  1. Arguments, taken from Right to Left
    a. Right Most
    b. 2nd from Right
  2. Base class
  3. Virtual base
  4. Base classes from Left to Right
  5. Members in Order of Declaration in class
  6. Constructor of called class
查看更多
欢心
3楼-- · 2019-01-15 08:18

1) First of all, it is needed to calculate the arguments.

2) Then base classes are constructed.

3) Then members are constructed in the order of appearance in the declaration of the class.

4) Then Constructor of X is called

查看更多
成全新的幸福
4楼-- · 2019-01-15 08:18
  1. The W object will be constructed before the constructor to X is called.
  2. Z, as a base class of X, will be initialized before the members of X.
  3. Y will be initalized during member initialization
  4. X's constructor will run.
查看更多
迷人小祖宗
5楼-- · 2019-01-15 08:30

To expand on Charles Bailey's answer, the rules change when your base classes are inherited virtually. I always forget what the order is, the IBM site says that virtual bases are initialized first but I've just never run into a case where it's actually more than trivia.

查看更多
Rolldiameter
6楼-- · 2019-01-15 08:31

In all classes construction order is guaranteed: base classes, as specified from left to right followed by member variables in the order declared in the class definition. A class's constructor body is executed once all of its bases' and members' constructions have completed.

In your example X is derived from Z and contains Y so the Z base object is constructed first, then the Y member y, then the construction of the X completes with the execution of X's constructor body.

The temporary W is needed to pass to the constructor of X, so it is constructed before the construction of the x begins and will be destroyed once the initialization of x completes.

So the program must print:

W
Z
Y
X
查看更多
登录 后发表回答