我不希望派生类继承基类的构造函数
I do not want the derived class to inherit the constructor function of the Base class
因为它位于标题处。
以下是示例:
#include <iostream>
using namespace std;
class Base {
private:
int nSize;
public:
Base(){
cout << "I'm Base constructor" << endl;
}
};
class Derived : public Base {
int nMaxSize;
public:
Derived(){
cout << "I'm Derived constructor" << endl;
}
};
int main(){
Derived obj;
return 0;
}
结果:
我是基地建设者我是派生构造函数
您别无选择。你能做的最好的事情就是向基本构造函数传递一个标志,禁用你不想要的东西。但这表明设计不好。
相关文章:
- C++17 使用驱动类常量作为基类构造函数的参数来初始化基类构造函数
- 派生类(构造函数具有参数)和基类(构造函数缺少参数)之间没有可行的转换
- 在成员构造函数之后调用基类构造函数
- 如何在将原始指针移动到基类构造函数之前从unique_ptr中提取原始指针
- C++:在共享对象中调用抽象基类构造函数/未定义的符号
- 如何在 C++ 中将参数传递给基类构造函数
- 将数据成员的指针传递给基类构造函数是否安全?
- 使用基类构造函数初始化儿童类
- 使用基类构造函数的子类构造函数的大纲定义
- MSVC 无法识别继承模板类的模板类的"直接"基类构造函数
- 子类的构造函数后跟冒号后的基类构造函数是什么意思?
- 是否可以使用基类构造函数通过派生类将值初始化为基类私有成员?
- 当派生类无法轻易将参数传递到基类时,如何调用基类构造函数
- 是否可以在运行时切换到不同的基类构造函数?
- 对基类构造函数的未定义引用
- 如何为基类构造函数中的每个子类执行特定任务
- 将类传递到基类构造函数的模板参数中?
- 如何在基类构造函数中使用派生类成员
- (为什么)纯虚拟派生类中是否需要虚拟基类构造函数调用?
- 使用相同的参数调用基类C++构造函数