gpt4 book ai didi

C++ 11 委托(delegate)构造函数纯虚方法和函数调用——危险?

转载 作者:IT老高 更新时间:2023-10-28 22:38:55 27 4
gpt4 key购买 nike

不是 Invoking virtual function and pure-virtual function from a constructor 的重复项:

以前的问题与 C++ 03 相关,而不是 C++ 11 中的新构造函数委托(delegate)行为,并且该问题没有解决通过使用委托(delegate)来缓解未定义行为的问题,以确保在执行纯虚拟实现之前正确构造。

在 C++ 11 中,在构造过程中,但在通过构造函数委托(delegate)“完全构造”类/对象之后,在类的构造函数中调用纯虚函数有什么危险?

显然,在 C++ 11 规范中的某处存在这样的约束,

Member functions (including virtual member functions, 10.3) can be called for an object under construction. Similarly, an object under construction can be the operand of the typeid operator .. - 12.6.2 #13 of the [C++ Working Draft] (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf) Can't find "Fair Use" version of Published Spec.

C++11 considers an object constructed once any constructor finishes execution. Since multiple constructors will be allowed to execute, this will mean that each delegate constructor will be executing on a fully constructed object of its own type. Derived class constructors will execute after all delegation in their base classes is complete. - Wikipedia saying that this is a C++ 11 thing.

实际 C++ 11 引用未知。

以下示例在 Visual Studio 2012 C++ 编译器的 Nov CTP 中编译并运行:

#include <string>

/**************************************/
class Base
{
public:
int sum;
virtual int Do() = 0;

void Initialize()
{
Do();
}
Base()
{
}
};

/**************************************/
// Optionally declare class as "final" to avoid
// issues with further sub-derivations.
class Derived final : public Base
{
public:

virtual int Do() override final
{
sum = 0 ? 1 : sum;
return sum / 2 ; // .5 if not already set.
}

Derived(const std::string & test)
: Derived() // Ensure "this" object is constructed.
{
Initialize(); // Call Pure Virtual Method.
}
Derived()
: Base()
{
// Effectively Instantiating the Base Class.
// Then Instantiating This.
// The the target constructor completes.
}
};




/********************************************************************/
int main(int args, char* argv[])
{
Derived d;
return 0;
}

最佳答案

随着更新,示例代码对我来说看起来不错,但需要注意的是,如果您创建了 Derived 的子类,则 Derived(const std::string &) 不会调用子类对 Do() 的覆盖,而是 Derived::Do() 仍然会被调用;这可能不是你想要的。特别是,当从 Derived(const std::string &) 构造函数调用 Initialize() 时,该对象仍然“仅”是 Derived 对象而不是 SubDerived 对象(因为构造代码的 SubDerived 层还没有尚未开始),这就是为什么会调用 Derived::Do() 而不是 SubDerived::Do()。

问:如果子类使用相同的委托(delegate)模式来确保一切都以相同的方式实例化怎么办?

答:这主要是可行的,但前提是可以在调用 SubDerived::Do() 之前调用 Derived::Do()。

特别是,假设您有一个 SubDerived 类,它与上面的 Derived 所做的事情相同。然后当调用代码这样做时:

SubDerived foo("Hello");

会发生以下调用序列:

Base()
Derived()
Derived(const std::string &)
Base::Initialize()
Derived::Do()
SubDerived()
SubDerived(const std::string &)
Base::Initialize()
SubDerived::Do()

...所以是的,SubDerived::Do() 最终会被调用,但 Derived::Do() 也会被调用。这是否会成为问题取决于各种 Do() 方法的实际作用。

一些建议:从构造函数中调用虚方法通常不是最好的方法。您可能需要考虑在构造对象后简单地要求调用代码在对象上手动调用 Do()。调用代码需要做更多的工作,但优点是可以避免在对部分构造的对象进行虚拟方法调用时发挥作用的不太明显或不方便的语义。

关于C++ 11 委托(delegate)构造函数纯虚方法和函数调用——危险?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14681349/

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