不从派生类调用基类构造函数

发布于 2024-09-29 20:21:20 字数 301 浏览 7 评论 0原文

假设我有一个基类:

class baseClass  
{  
  public:  
baseClass() { };

};

和一个派生类:

class derClass : public baseClass
    {  
      public:  
    derClass() { };

    };

当我创建 derClass 的实例时,将调用 baseClass 的构造函数。我怎样才能防止这种情况发生?

Say I have a base class:

class baseClass  
{  
  public:  
baseClass() { };

};

And a derived class:

class derClass : public baseClass
    {  
      public:  
    derClass() { };

    };

When I create an instance of derClass the constructor of baseClass is called. How can I prevent this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

拥抱我好吗 2024-10-06 20:21:21

您可能想要创建一个受保护的(可能为空)默认构造函数,该构造函数将由派生类使用:

class Base {
protected:
  Base() {}

public:
  int a;
  
  Base(int x) {a = 12;}
};

class Derived : Base {
public:
  Derived(int x) {a = 42;}
};

这将阻止任何外部代码使用默认 Base 构造函数(因此不会初始化 a< /code> 正确)。您只需要确保在 Derived 构造函数中初始化所有 Base 字段即可。

You might want to create a protected (possibly empty) default constructor which would be used by the derived class:

class Base {
protected:
  Base() {}

public:
  int a;
  
  Base(int x) {a = 12;}
};

class Derived : Base {
public:
  Derived(int x) {a = 42;}
};

This will block any external code from using default Base constructor (and thus not initializing a properly). You just need to make sure you initialize all the Base fields in the Derived constructor as well.

以往的大感动 2024-10-06 20:21:20

创建一个额外的空构造函数。

struct noprapere_tag {};

class baseClass  
{  
public:  
  baseClass() : x (5), y(6) { };

  baseClass(noprapere_tag()) { }; // nothing to do

protected:
  int x;
  int y;

};

class derClass : public baseClass
{  
public:  
    derClass() : baseClass (noprapere_tag) { };

};

Make an additional empty constructor.

struct noprapere_tag {};

class baseClass  
{  
public:  
  baseClass() : x (5), y(6) { };

  baseClass(noprapere_tag()) { }; // nothing to do

protected:
  int x;
  int y;

};

class derClass : public baseClass
{  
public:  
    derClass() : baseClass (noprapere_tag) { };

};
猫卆 2024-10-06 20:21:20

基类实例是任何派生类实例的组成部分。如果成功构造派生类实例,根据定义,您必须构造所有基类和成员对象,否则派生对象的构造将会失败。构造基类实例涉及调用其构造函数之一。

这是继承在 C++ 中如何工作的基础。

A base class instance is an integral part of any derived class instance. If you successfully construct a derived class instance you must - by definition - construct all base class and member objects otherwise the construction of the derived object would have failed. Constructing a base class instance involves calling one of its constructors.

This is fundamental to how inheritance works in C++.

我不是你的备胎 2024-10-06 20:21:20

工作程序

#include <iostream>

using namespace std;
class A
{
    public:
    A()
    {
        cout<<"a\n";
    }
    A(int a)
    {}
};
class B:public A
{
    public:
    B() : A(10)
    {
        cout<<"b\n";
    }
   
};
int main()
{
    
    new A;
    cout<<"----------\n";
    new B;
    
    return 0;
}

输出示例

a                                                                                                        
----------                                                                                               
b   

Sample working program

#include <iostream>

using namespace std;
class A
{
    public:
    A()
    {
        cout<<"a\n";
    }
    A(int a)
    {}
};
class B:public A
{
    public:
    B() : A(10)
    {
        cout<<"b\n";
    }
   
};
int main()
{
    
    new A;
    cout<<"----------\n";
    new B;
    
    return 0;
}

output

a                                                                                                        
----------                                                                                               
b   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文