- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
不是 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/
谁能解释一下原因: (define a (lambda() (cons a #f))) (car (a)) ==> procedure ((car (a))) ==> (procedure . #f)
这是 PyBrain 网站的摘录。我了解大部分正在发生的事情,但是一行让我完全难住了。我以前从未在 python 代码中看到过这样的东西。这是整个循环,对于上下文: for c in [0,
我是gradle / groovy的新手。我想创建将做一些事情的自定义任务。我的第一个问题是任务完成时该如何做?我可以覆盖doFirst / doLast闭包吗?也许我可以重写某些在开始和结束时都会执
我刚刚开始评估 MS 企业库。他们使用以下指令来获取实例: var customerDb = EnterpriseLibraryContainer.Current.GetInstance("C
这是我的 if else Ansible 逻辑.. - name: Check certs exist stat: path=/etc/letsencrypt/live/{{ rootDomain
我正在使用construct 2.8 对一些失传已久的 Pascal 程序创建的一些文件的 header 进行逆向工程。 header 由许多不同的记录组成,其中一些是可选的,我不确定顺序是否固定。
我在将 getchar() 的输入放入 char *arr[] 数组时遇到问题。我这样做的原因是因为输入数据(将是一个带有命令行参数的文件)将存储在一个 char 指针数组中以传递给 execvp 函
通常我们不能约束类型参数 T派生自密封类型(例如 struct 类型)。这将毫无意义,因为只有一种类型适合,因此不需要泛型。所以约束如下: where T : string 或: where T :
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve th
#include using namespace std; class A { private: int m_i; friend int main(int argc, char cons
这个问题在这里已经有了答案: Are there legitimate uses for JavaScript's "with" statement? (33 个答案) 关闭 9 年前。 我有这个代
在this answer我看到了下一个 Bash 结构。 yes "$(< file.txt)" 什么意思 "$(< file.txt)" ? 我明白了 命令替换 - $(command)用命令的结
if (a == 1) //do something else if (a == 2) //do something else if (a == 3) //do somethi
关于构造的快速简单的问题。 我有以下用于将项目添加到 ListView 的代码。 ListViewItem item = new ListViewItem(); item.Text = file; i
我想使用 std::vector 来控制给定的内存。首先,我很确定这不是好的做法,但好奇心占了上风,无论如何我都想知道如何做到这一点。 我遇到的问题是这样的方法: vector getRow(unsi
下面显示了一段简单的javascript: var mystring = ("random","ignored","text","h") + ("ello world") 这个字符串会生成 hello
在 Java 中,创建对象的标准方法是使用 MyClass name = new MyClass(); 我也经常看到构造 new MyClass() { /*stuff goes in here*/
我正在编写 C++ ndarray 类。我需要动态大小和编译时大小已知的数组(分别分配自由存储和分配堆栈)。我想支持从嵌套的 std::initializer_list 进行初始化。 动态大小的没问题
我正在将一个项目从 Visual Studio 2005 转换为 Visual Studio 2008,并提出了上述结构。 using Castle.Core.Resource; using Cast
我想知道我在这里的想法是否正确,我主要针对接口(interface)进行编程,所以我想知道下面的类是否应该通过 DI 注入(inject),或者我应该自己实例化一个类... 注意:这些服务保存在我的核
我是一名优秀的程序员,十分优秀!