简体   繁体   English

实例化派生类对象,其基类ctor是私有的

[英]Instantiate a derived class object, whose base class ctor is private

How to instantiate a derived class object, whose base class ctor is private? 如何实例化一个派生类对象,其基类ctor是私有的?

Since the derived class ctor implicitly invokes the base class ctor(which is private), the compiler gives error. 由于派生类ctor隐式调用基类ctor(私有),编译器会给出错误。

Consider this example code below: 请考虑以下示例代码:

#include <iostream>

using namespace std;

class base
{
   private:
      base()
      {
         cout << "base: ctor()\n";
      }
};

class derived: public base
{
   public:
      derived()
      {
         cout << "derived: ctor()\n";
      }
};

int main()
{
   derived d;
}

This code gives the compilation error: 此代码给出了编译错误:

accessing_private_ctor_in_base_class.cpp: In constructor derived::derived()': accessing_private_ctor_in_base_class.cpp:9: error: base::base()' is private accessing_private_ctor_in_base_class.cpp:18: error: within this context access_private_ctor_in_base_class.cpp:在构造函数derived::derived()': accessing_private_ctor_in_base_class.cpp:9: error: base :: base()'是private acces_private_ctor_in_base_class.cpp:18:错误:在此上下文中

How can i modify the code to remove the compilation error? 如何修改代码以删除编译错误?

There are two ways: 有两种方法:

  • Make the base class constructor either public or protected . 使基类构造函数为publicprotected
  • Or, make the derived class a friend of the base class. 或者,使派生类成为基类的friend see demo 看看演示

You can't inherit from a base-class whose only constructor is private. 您不能从只有构造函数是私有的基类继承。 1 1

So make the base-class constructor public/protected, or add another base-class constructor. 因此,将基类构造函数设置为public / protected,或者添加另一个基类构造函数。


1. Unless, as Nawaz points out, you are a friend of the base class. 除非,正如纳瓦兹指出的那样,你是基地的朋友。

You can't. 你不能。 That's usually the reason to make the only c'tor private, disallow inheritance. 这通常是使唯一的c'tor私有,不允许继承的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将使用基类ctor创建的对象转换为派生类 - convert an object created with base class ctor to a derived class 在派生类中使用基类Copy CTOR - Use base class Copy CTOR in derived class 在派生类中使用虚拟基类中的受保护ctor - use protected ctor from virtual base class in derived class 派生类中基类委派的ctor的c ++使用 - c++ usage of delegated ctor from base class in derived class 返回基数为singleton的派生类的objec - Return objec of derived class whose base is singleton 虚拟继承:Base Ctor没有调用Most Derived Class? - Virtual Inheritance : Base Ctor not calling in Most Derived Class? “最派生类的ctor需要直接调用虚拟基类的ctor”语句的原始内容在哪里? - Where is the original of the statement “the most derived class's ctor needs to directly call the virtual base class's ctor”? 如何使用派生Ctor中模板基类的typedef? - How to use typedef from template base class in Ctor of the Derived? The direct base class of private inheritance prevents the derived class from defining the object of the indirect base class - The direct base class of private inheritance prevents the derived class from defining the object of the indirect base class C ++如何创建创建派生类对象的基类,并访问基类的私有成员? - C++ How to create base class which creates derived class object, and access private members of base class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM