gpt4 book ai didi

c++ - 使用对象表达式在构造函数中调用虚函数

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:10:41 27 4
gpt4 key购买 nike

代码:

#include <iostream>

using std::cout;
using std::endl;

struct A
{
virtual void foo()
{
cout << "A" << endl;
}

A(){ }
};

struct B : A
{
B();
virtual void foo()
{
cout << "B" << endl;
}
};

B b;

B::B()
{
b.foo();
foo();
}

struct C : B
{
virtual void foo()
{
cout << "C" << endl;
}

C() : B(){ }
};

C c;

int main(){ }

DEMO

When a virtual function is called directly or indirectly from a constructor or from a destructor, including during the construction or destruction of the class’s non-static data members, and the object to which the call applies is the object (call it x) under construction or destruction, the function called is the final overrider in the constructor’s or destructor’s class and not one overriding it in a more-derived class. If the virtual function call uses an explicit class member access (5.2.5) and the object expression refers to the complete object of x or one of that object’s base class subobjects but not x or one of its base class subobjects, the behavior is undefined.

我一直在尝试接收 UB

If the virtual function call uses an explicit class member access (5.2.5) and the object expression refers to the complete object of x [...]

不清楚x 的完整对象 是什么意思,其中x 是一个对象。是否与x类型的完整对象相同?

最佳答案

§1.8 [intro.object]/p2-3:

Objects can contain other objects, called subobjects. A subobject can be a member subobject (9.2), a base class subobject (Clause 10), or an array element. An object that is not a subobject of any other object is called a complete object.

For every object x, there is some object called the complete object of x, determined as follows:

  • If x is a complete object, then x is the complete object of x.
  • Otherwise, the complete object of x is the complete object of the (unique) object that contains x.

从本质上讲,您引用的句子是做 static_cast<C*>(this)->foo();B的构造函数在您的代码中未定义行为,即使正在构造的完整对象是 C .该标准实际上在这里提供了一个很好的例子:

struct V {
virtual void f();
virtual void g();
};
struct A : virtual V {
virtual void f();
};
struct B : virtual V {
virtual void g();
B(V*, A*);
};

struct D : A, B {
virtual void f();
virtual void g();
D() : B((A*)this, this) { }
};

B::B(V* v, A* a) {
f(); // calls V::f, not A::f
g(); // calls B::g, not D::g
v->g(); // v is base of B, the call is well-defined, calls B::g
a->f(); // undefined behavior, a’s type not a base of B
}

事实上,你已经可以see the undefined behavior show up in this example if you run it : Ideone 的编译器 (GCC) 实际上调用了 V::f()a->f(); 上行,即使指针指的是一个完全构建的 A子对象。

关于c++ - 使用对象表达式在构造函数中调用虚函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25562042/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com