我不希望派生类继承基类的构造函数

I do not want the derived class to inherit the constructor function of the Base class

本文关键字:基类 构造函数 继承 希望 派生      更新时间:2023-10-16

因为它位于标题处。

以下是示例:

#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;
}

结果:

我是基地建设者我是派生构造函数

您别无选择。你能做的最好的事情就是向基本构造函数传递一个标志,禁用你不想要的东西。但这表明设计不好。