C++ 覆盖或删除继承的构造函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3156597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 12:13:20  来源:igfitidea点击:

Override or remove an inherited constructor

c++classinheritance

提问by gokoon

Here is some C++ code:

下面是一些 C++ 代码:

#include <iostream>
using namespace std;

class m
{
    public:
    m() { cout << "mother" << endl; }
};

class n : m
{
    public:
    n() { cout << "daughter" << endl; }
};

int main()
{
    m M;
    n N;
}

Here is the output:

这是输出:

mother  
mother  
daughter

My problem is that I don't want the m's constructor to be called when I create N. What should I do ?

我的问题是我不希望在创建 N 时调用 m 的构造函数。我该怎么办?

回答by SigTerm

AFAIK, you cannot remove inherited constructor.

AFAIK,您不能删除继承的构造函数。

The problem in your example comes from incorrect class design. Constructor is normally used for allocating class resources, setting default values, and so on. It is not exactly suitable to be used for outputting something.

您示例中的问题来自不正确的类设计。构造函数通常用于分配类资源、设置默认值等。它并不完全适合用于输出某些东西。

You should put

你应该把

n() { cout << "daughter" << endl; }

Into virtual function.

进入虚函数。

In general - if you have a need to remove inherited constructor, then you probably need to rethink/redesign your class hierarchy.

一般来说 - 如果您需要删除继承的构造函数,那么您可能需要重新思考/重新设计您的类层次结构。

回答by adf88

class m
{
public:
      m(bool init = true) { if (init) cout << "mother" << endl; }
};


class n : m
{
public:
      n() : m(false) { cout << "daughter" << endl; }
};

or if you don't want it to be public

或者如果你不希望它是公开的

class m
{
protected:
    m(bool init) { if(init) Init(); }
    Init() { cout << "mother" << endl; }

public:
      m() { Init(); }
};

class n : m
{
public:
      n() : m(false) { cout << "daughter" << endl; }
};

回答by Frerich Raabe

Two solutions:

两种解决方案:

  1. Don't derive nfrom m. Check that you really have interface reuse (that you rely on a polymorphic interface) instead of implementation reuse. In the latter case, prefer making an m*a member of nand then only create the mobject when needed. This would be my preferred solution.

  2. You probably don't want ms contructor to be called because it does something which you don't want. Move that code which you don't want to execute out of ms constructor into a dedicated init()function or the like, and then call it as needed. I don't recommend this because you end up with a stateful interface.

  1. 不要nm. 检查您是否真的有接口重用(您依赖于多态接口)而不是实现重用。在后一种情况下,更喜欢创建m*一个成员,n然后仅m在需要时创建对象。这将是我首选的解决方案。

  2. 您可能不希望m调用 s 构造函数,因为它执行了您不想要的操作。将您不想从ms 构造init()函数中执行的代码移动到专用函数等中,然后根据需要调用它。我不推荐这样做,因为你最终会得到一个有状态的接口。

回答by Philipp

Constructors are never inherited. What happens is that C++ generates a default nullary constructor that initializes the base classes and members of class type. The base classes are always initialized and there is no way to prevent this, so if you don't want the base class constructors to be called, don't inherit from the base class.

构造函数永远不会被继承。发生的事情是 C++ 生成一个默认的空构造函数来初始化基类和类类型的成员。基类总是被初始化并且没有办法阻止这种情况,因此如果您不想调用基类构造函数,请不要从基类继承。

回答by Tadeusz Kopec

Object of any class contains in it sub-objects of all its superclasses. And all of them mustbe constructed before construction of main object. Part of this construction is calling one of base class constructors and it cannot be omitted. You can only choose constructor to be called.

任何类的对象都包含其所有超类的子对象。并且所有这些都必须在构建主要对象之前构建。此构造的一部分是调用基类构造函数之一,并且不能省略。您只能选择要调用的构造函数。