简体   繁体   English

为什么在声明派生类时需要在基类中将变量设置为某个值?

[英]Why I need to set a variable to some value in base class when I declare a derived class?

I dont understand why, when i try to create a object of a base class, i can, but only without declaring derived class, and when declare Derived i need to set argument value to "= 0" in base class constructor? 我不明白为什么,当我尝试创建基类的对象时,我可以但只能在不声明派生类的情况下进行操作,而当声明派生类时,我需要在基类构造函数中将参数值设置为“ = 0”?

#include <cstdlib>

using namespace std;

class Base {
   public:
       int m_nValue;

       Base(int nValue=0) //<--- why here i need = 0 ?
           : m_nValue(nValue)
       {}
};

class Derived: public Base {
public:
    double m_dValue;

    Derived(double dValue) 
        : m_dValue(dValue)
    {}
};


int main(int argc, char** argv) {
  Base cBase(5); // use Base(int) constructor
  return 0;
}

No you don't. 不,你不会。 The problem is that when you subclass, you never give any value to the base class ctor, something like this: 问题是,当您子类化时,您永远不会给基类ctor赋予任何值,如下所示:

Derived(double dValue) : Base(0), m_dValue(dValue) {}

So the compiler looks at the base class and search either: a ctor with no argument, or a ctor with default values (he doesn't look at the default ctor as defining a ctor removes the default ctor). 因此,编译器查看基类并搜索:不带参数的ctor或具有默认值的ctor(他不查看默认ctor,因为定义ctor会删除默认ctor)。 Either: 要么:

class Base {
public:    
    int m_nValue;   
    Base() : m_nValue(0) {}
    Base(int nValue) : m_nValue(nValue) {}
};

or 要么

class Base {
public:    
    int m_nValue;   
    Base(int nValue=0) : m_nValue(nValue) {}
};

Base(int nValue = 0) will also act as the default constructor, since a default value for nValue has been supplied. Base(int nValue = 0)也将充当默认构造函数,因为已经提供了nValue的默认值。 Currently, your Derived constructor will call this constructor implicitly, with nValue set to 0. 当前,您的Derived构造函数将隐式调用此构造函数,并将nValue设置为0。

Formally you don't actually need to do that - but that would mean that the user of your class would have to supply an explicit value for nValue and you would have to write a default constructor . 形式上您实际上并不需要这样做-但这意味着您的类的用户必须为nValue提供一个显式的值,并且您必须编写一个默认的构造函数 You could achieve the latter by writing Base() = default; 您可以通过编写Base() = default;来实现后者Base() = default; although then the member m_nValue will not be initialised unless you use the C++11 curly brace notation (eg Base b{}; ). 但是, 除非您使用C ++ 11大括号表示法(例如Base b{}; ), 否则不会初始化成员m_nValue (Note that the behaviour on reading an uninitialised variable is undefined .) (请注意,读取未初始化变量的行为是undefined 。)

暂无
暂无

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

相关问题 我应该在派生类或基类的哪里声明朋友类? - Where in derived class or base class should I declare friend class? 当我在基类中设置受保护的数据时,它将更改派生类中受保护的数据的值吗? - When I set protected data in a base class will it change the value of the protected data in the derived class? 如何通过使用基类对象设置派生类变量的值? - How to set the value of a variable of derived class by using the base class object? 在构造派生类以定义某种大类型的基类成员时,应如何传递值? - When constructing a derived class which defines a base class member of some large type, how should I pass the value? 我需要通过基类静态变量访问派生类成员 - I need to access a derived class member through the base class static variable 基类指针仅获得派生类变量值,而不得到基类变量值,为什么? - Base Class Pointer only getting a Derived class variable value and not the base class variable value why? 在模板派生类中,为什么我需要在成员函数中使用“this-&gt;”来限定基类成员名称? - In a templated derived class, why do I need to qualify base class member names with "this->" inside a member function? 虚拟继承(钻石)-为什么需要从派生程度最高的类上转换为基类 - Virtual inheritance (diamond) - Why do I need to upcast to Base class from the most Derived class 当基类指针指向基类中声明的派生类虚拟函数时,为什么会出现编译时错误? - Why do I get compile time error when base class pointer points to derived class virtual function that is declared in base class? 如何转发声明从转发声明的模板库 class 派生的 class? - How do I forward declare a class derived from a forward declared template base class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM