C++使用将在派生构造函数中构造的参数调用基构造函数
问题 1)
class Base {
Base(std::string name);
virtual std::string generateName();
}
class Derived : Base {
Derived();
virtual std::string generateName();
}
问题来了:
generateName() 上将调用什么方法?
Derived :: Derived : Base(generateName()) {
//what method will be called on generateName() ?
}
问题2)
我应该怎么做?如果默认构造函数必须接受参数,但我需要在派生构造函数中生成该参数?
QUESTION 1)
class Base {
Base(std::string name);
virtual std::string generateName();
}
class Derived : Base {
Derived();
virtual std::string generateName();
}
here comes the question :
what method will be called on generateName() ?
Derived :: Derived : Base(generateName()) {
//what method will be called on generateName() ?
}
QUESTION 2)
how should i make it? if the default constructor must accept a parameter, but i need to generate that parameter in the Derived constructor?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,解决方案:使用静态成员函数或非成员函数。
至于行为,将调用
Derived::generateName()
。 C++ 标准中定义此行为的长句说道 (C++03 12.7/3):由于虚拟调用时执行的构造函数是
Derived
构造函数,因此会调用Derived::generateName()
。现已删除的答案正确地引用了 Scott Meyers 的一篇文章,该文章建议“在构造或过程中切勿调用虚拟函数”销毁。” 调用覆盖者的规则非常复杂且难以记住。
First, the solution: use a static member function or a nonmember function.
As for the behavior,
Derived::generateName()
will be called. The long sentence in the C++ Standard that defines this behavior says (C++03 12.7/3):Because the constructor being executed at the time of the virtual call is the
Derived
constructor,Derived::generateName()
is called.A now-deleted answer rightly referred to an article by Scott Meyers that recommends "Never Call Virtual Functions during Construction or Destruction." The rules for what overrider gets called are complex and difficult to remember.
采取两个...
我在基类初始化程序和两个构造函数中调用了
generateName()
进行了一次运行。输出让我感到困惑:我从未想象过一个类可以在单个构造序列中从派生类转变为基类,然后又变回派生类。你每天都会学到新东西。
Take two...
I did a run with calls to
generateName()
in the base class initialiser and both constructors. The output left me nonplussed:I never imagined that a class could morph from being a derived to a base, then back to a derived in a single construction sequence. You learn something new every day.