为什么我的C ++子类需要一个显式的构造函数? [英] Why does my C++ subclass need an explicit constructor?

查看:212
本文介绍了为什么我的C ++子类需要一个显式的构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基类声明和定义一个构造函数,但由于某种原因,我的公开派生类没有看到该构造函数,因此我必须在派生类中显式声明一个转发构造函数:

  class WireCount0 {
protected:
int m;
public:
WireCount0(const int& rhs){m = rhs; }
};

class WireCount1:public WireCount0 {};

class WireCount2:public WireCount0 {
public:
WireCount2(const int& rhs):WireCount0(rhs){}
};

int dummy(int argc,char * argv []){
WireCount0 wireCount0(100);
WireCount1 wireCount1(100);
WireCount2 wireCount 2(100);
return 0;
}



在上面的代码中,我的 WireCount1 wireCount1 ) 和 wireCount2 声明很好。



我不知道我明白为什么我需要提供显式的构造函数如 WireCount2 中所示。是因为编译器生成 WireCount1 的默认构造函数,并且该构造函数隐藏了 WireCount0 构造函数



作为参考,编译器是 i686-apple-darwin10-gcc-4.2.1(GCC)4.2.1(Apple Inc. build 5659)



当您创建一个重载的构造函数时,默认的编译器生成的无参数构造函数会消失,派生类必须调用重载的构造函数。



当你有这样的东西时:

  class Class0 {
}
$ b b class Class1:public Class0 {
}

编译器实际上生成:

  class Class0 {
public:
Class0(){}
}

class Class1:public Class0 {
Class1():Class0(){}
}



当你有非默认的构造函数,无参数的构造函数不再生成。当你定义如下:

  class Class0 {
public:
Class0(int param){}
}

class Class1:public Class0 {
}

编译器不再在Class1中生成一个构造函数来调用基类的构造函数,您必须自己明确这样做。


I have a base class that declares and defines a constructor, but for some reason my publicly derived class is not seeing that constructor, and I therefore have to explicitly declare a forwarding constructor in the derived class:

class WireCount0 {
protected:
    int m;
public:
    WireCount0(const int& rhs) { m = rhs; }
};

class WireCount1 : public WireCount0 {};

class WireCount2 : public WireCount0 {
public: 
  WireCount2(const int& rhs) : WireCount0(rhs) {}
};

int dummy(int argc, char* argv[]) {
    WireCount0 wireCount0(100);
    WireCount1 wireCount1(100);
    WireCount2 wireCount2(100);
    return 0;
}

In the above code, my WireCount1 wireCount1(100) declaration is rejected by the compiler ("No matching function for call to 'WireCount1::WireCount1(int)'"), while my wireCount0 and wireCount2 declarations are fine.

I'm not sure that I understand why I need to provide the explicit constructor shown in WireCount2. Is it because the compiler generates a default constructor for WireCount1, and that constructor hides the WireCount0 constructor?

For reference, the compiler is i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5659).

解决方案

All the derived classes must call their base class's constructor in some shape or form.

When you create an overloaded constructor, your default compiler generated parameterless constructor disappears and the derived classes must call the overloaded constructor.

When you have something like this:

class Class0 {
}

class Class1 : public Class0 {
}

The compiler actually generates this:

class Class0 {
public:
  Class0(){}
}

class Class1 : public Class0 {
  Class1() : Class0(){}
}

When you have non-default constructor, the parameterless constructor is no longer generated. When you define the following:

class Class0 {
public:
  Class0(int param){}
}

class Class1 : public Class0 {
}

The compiler no longer generates a constructor in Class1 to call the base class's constructor, you must explicitly do that yourself.

这篇关于为什么我的C ++子类需要一个显式的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆