相关疑难解决方法(0)

C++ Base构造函数使用将在派生构造函数中构造的参数进行调用

问题1)

class Base {
    Base(std::string name);

    virtual std::string generateName();
}

class Derived : Base {
    Derived();

    virtual std::string generateName();
}
Run Code Online (Sandbox Code Playgroud)

这里有一个问题:

将在generateName()上调用什么方法?

Derived :: Derived : Base(generateName()) {
    //what method will be called on generateName() ? 
}
Run Code Online (Sandbox Code Playgroud)

问题2)

我该怎么做?如果默认构造函数必须接受一个参数,但我需要在Derived构造函数中生成该参数?

c++ oop constructor virtual-functions class

5
推荐指数
1
解决办法
685
查看次数

标签 统计

c++ ×1

class ×1

constructor ×1

oop ×1

virtual-functions ×1