我不明白为什么这个程序的输出如下。为什么没有编译错误?我想当尝试构造B时,编译器将找不到名为foo()的函数并报告错误。
#include <iostream>
using namespace std;
struct A{
int a;
A(int i=0) : a(i) { cout << "A" << endl; }
~A() { cout << "Bye A" << endl; }
int foo() { return a; }
};
struct B{
int b;
B(int i=0) : b(i) { cout << "B" << endl; }
~B() { cout << "Bye B" << endl; }
int bar() { return b; }
};
struct C : B, A {
C(int i=0) : B(foo()), A(i) {}
};
int main() {
cout << C(10).bar() << endl;
return 0;
}
输出:
B
A
0
Bye A
Bye B
一般来说,我想知道什么时候有多个继承,父结构的构造和初始化顺序是什么?我也可以期待类中的类似行为吗?
非常感谢有关构造函数和析构函数调用顺序的任何解释。
注意:这不是作业。而且,我已经研究了类似的主题,但没有提出任何关于这个问题。
答案 0 :(得分:16)
您在完全初始化对象之前调用foo
来调用未定义的行为。引用C ++标准中的12.6.2:
可以为正在构建的对象调用成员函数(包括虚拟成员函数,10.3)。 类似地,正在构造的对象可以是
typeid
运算符(5.2.8)或dynamic_cast
(5.2.7)的操作数。但是,如果这些操作是在 ctor-initializer (或直接调用的函数)中执行的 或者在基类的所有 mem-initializers 完成之前,间接来自 ctor-initializer ,结果 操作未定义。 [例如:
class A {
public:
A(int);
};
class B : public A {
int j;
public:
int f();
B() : A(f()), // undefined: calls member function
// but base A not yet initialized
j(f()) { } // well-defined: bases are all initialized
};
class C {
public:
C(int);
};
class D : public B, C {
int i;
public:
D() : C(f()), // undefined: calls member function
// but base C not yet initialized
i(f()) { } // well-defined: bases are all initialized
};
- 结束示例]
换句话说,根据标准,这是可以的:
C(int i=0) : B(), A(i) {
B::b = foo();
}
这将打印10
而不是您获得的0
(这可能是其他任何内容,因为这是未定义的行为)。
抛开未定义行为的问题,并解决您的问题,初始化发生的顺序是明确定义的:
在非委托构造函数中,初始化按以下顺序进行:
- 首先,只有最派生类(1.8)的构造函数,才初始化虚拟基类 它们出现在基类的有向无环图的深度优先从左到右遍历中的顺序, 其中“从左到右”是派生类 base-specifier-list 中基类出现的顺序。
- 然后,直接基类按声明顺序初始化,因为它们出现在 base-specifier-list 中 (无论 mem-initializers 的顺序如何)。
- 然后,按照在类定义中声明的顺序初始化非静态数据成员 (无论 mem-initializers 的顺序如何)。
- 最后,执行构造函数体的复合语句。
[注意:声明命令的目的是确保基础和成员子对象在 初始化的逆序。 - 结束说明]
因此,在您的代码中,初始化顺序为:B
(B::b
),A
(A::a
),C
()。
如下面的评论中所述,但是更改此初始化顺序(例如,使用struct C : A, B
而不是struct C : B, A
)将不会消除未定义的行为。即使初始化A::foo
部分,在初始化B
部分之前调用A
仍未定义。
答案 1 :(得分:5)
这只是未定义行为的另一种情况。例如,我的系统给出了以下结果。
B
A
-858993460
Bye A
Bye B
试试这个live demo会产生另一个不同的结果(C(10).bar()
生成32764)。
foo()
,但在 A
的构造函数之前它将被称为。这意味着a
被初始化,这导致读取未初始化的变量,这导致未定义的行为。这类似于在初始化之前访问成员。请考虑以下示例。 a
初始化为b
的值,然后初始化b
。问题很明显,b
在读取初始化a
时未初始化。
struct foo
{
foo(int x) : a(b), b(x) {}
int a;
int b;
};
int main()
{
foo bar(10);
}
答案 2 :(得分:0)
我想看看实际发生了什么。既然有了结果,这里就是给你的。
#include <iostream>
using namespace std;
/*** Structures ***/
struct A{
int a;
#warning a integer declared in struct A
A(int i=0) : a(i)
#warning A constuctor declared
#warning A constructor creates variable i, sets it to 0
#warning then assigns i to a
{
cout << "inside constructor A" << endl;
cout << "i = " << i << endl;
cout << "a = " << a << endl;
cout << "leaving constructor A" << endl;
}
#warning A constructor definition provided
~A()
#warning A destructor declared
{
cout << "inside destructor A" << endl;
cout << "leaving destructor A" << endl;
}
#warning A destructor definition provided
int foo()
#warning foo function declared in struct A
{
cout << "inside foo, inside A" << endl;
cout << "foo will return a = " << a << " then leave foo" << endl;
return a;
}
#warning foo function defined in struct A
};
struct B{
int b;
#warning b integer declared in struct B
B(int i=0) : b(i)
#warning B constructor declared
#warning B creates int i and initializes it to 0
#warning b is assigned the value of i
{
cout << "inside constructor B" << endl;
cout << "i = " << i << endl;
cout << "b = " << b << endl;
cout << "leaving constructor B" << endl;
}
#warning B constructor defined
~B()
#warning B destructor declared
{
cout << "inside destructor B" << endl;
cout << "leaving destructor B" << endl;
}
#warning B destructor defined
int bar()
#warning bar function declared in struct B
{
cout << "inside bar, inside B" << endl;
cout << "bar will return b = " << b << " then leave bar" << endl;
return b;
}
#warning bar function defined in struct B
};
struct C : B, A
#warning C structure declared derived from B and A
{
C(int i=0) : B(foo()), A(i)
#warning C constructor declared
#warning C constructor creates int i and assigns value 0
#warning C constructor instantiates B and calls foo from A to assign value?
#warning C constructor instantiates A by assigning i to it
{
cout << "inside constructor C" << endl;
cout << "i = " << i << endl;
cout << "leaving constructor C" << endl;
}
#warning C constructor defined with no implementation
};
int main() {
cout << "command is: print the value of C(10).bar()" << endl;
cout << C(10).bar() << endl;
#warning initialize C with a value of 10
#warning then call the bar function extended from B
#warning declare struct C blah, initialized with C(12)
cout << endl << "creating struct blah with definition C(12)" << endl;
struct C blah = C(12);
cout << "calling blah.foo" << endl;
cout << blah.foo() << endl;
cout << "calling blah.bar" << endl;
cout << blah.bar() << endl;
#warning printing and then returning 0
cout << endl << "Some random output before returning 0" << endl;
return 0;
}
在编译结果过程中给出以下内容(稍微整理一下):
>make
test.cpp:7:2: warning: #warning a integer declared in struct A [-Wcpp]
test.cpp:10:2: warning: #warning A constuctor declared [-Wcpp]
test.cpp:11:2: warning: #warning A constructor creates variable i, sets it to 0 [-Wcpp]
test.cpp:12:2: warning: #warning then assigns i to a [-Wcpp]
test.cpp:19:2: warning: #warning A constructor definition provided [-Wcpp]
test.cpp:21:2: warning: #warning A destructor declared [-Wcpp]
test.cpp:26:2: warning: #warning A destructor definition provided [-Wcpp]
test.cpp:28:2: warning: #warning foo function declared in struct A [-Wcpp]
test.cpp:34:2: warning: #warning foo function defined in struct A [-Wcpp]
test.cpp:39:2: warning: #warning b integer declared in struct B [-Wcpp]
test.cpp:41:2: warning: #warning B constructor declared [-Wcpp]
test.cpp:42:2: warning: #warning B creates int i and initializes it to 0 [-Wcpp]
test.cpp:43:2: warning: #warning b is assigned the value of i [-Wcpp]
test.cpp:50:2: warning: #warning B constructor defined [-Wcpp]
test.cpp:52:2: warning: #warning B destructor declared [-Wcpp]
test.cpp:57:2: warning: #warning B destructor defined [-Wcpp]
test.cpp:59:2: warning: #warning bar function declared in struct B [-Wcpp]
test.cpp:65:2: warning: #warning bar function defined in struct B [-Wcpp]
test.cpp:70:2: warning: #warning C structure declared derived from B and A [-Wcpp]
test.cpp:73:2: warning: #warning C constructor declared [-Wcpp]
test.cpp:74:2: warning: #warning C constructor creates int i and assigns value 0 [-Wcpp]
test.cpp:75:2: warning: #warning C constructor instantiates B and calls foo from A to assign value? [-Wcpp]
test.cpp:76:2: warning: #warning C constructor instantiates A by assigning i to it [-Wcpp]
test.cpp:82:2: warning: #warning C constructor defined with no implementation [-Wcpp]
test.cpp:88:2: warning: #warning initialize C with a value of 10 [-Wcpp]
test.cpp:89:2: warning: #warning then call the bar function extended from B [-Wcpp]
test.cpp:91:2: warning: #warning declare struct C blah, initialized with C(12) [-Wcpp]
test.cpp:98:2: warning: #warning printing and then returning 0 [-Wcpp]
并给出以下输出:
>test
command is: print the value of C(10).bar()
inside foo, inside A
foo will return a = 4201198 then leave foo
inside constructor B
i = 4201198
b = 4201198
leaving constructor B
inside constructor A
i = 10
a = 10
leaving constructor A
inside constructor C
i = 10
leaving constructor C
inside bar, inside B
bar will return b = 4201198 then leave bar
4201198
inside destructor A
leaving destructor A
inside destructor B
leaving destructor B
creating struct blah with definition C(12)
inside foo, inside A
foo will return a = 4201104 then leave foo
inside constructor B
i = 4201104
b = 4201104
leaving constructor B
inside constructor A
i = 12
a = 12
leaving constructor A
inside constructor C
i = 12
leaving constructor C
calling blah.foo
inside foo, inside A
foo will return a = 12 then leave foo
12
calling blah.bar
inside bar, inside B
bar will return b = 4201104 then leave bar
4201104
Some random output before returning 0
inside destructor A
leaving destructor A
inside destructor B
leaving destructor B