简体   繁体   English

c++派生类默认构造函数

[英]c++ derived class default constructor

if we compiled the following code, the compile would complain "no matching function for call to 'fruit::fruit()' "如果我们编译以下代码,编译器会抱怨“没有匹配的函数来调用‘fruit::fruit()’”

i think the reason is : apple constructor call a default fruit constructor and it can't find one我认为原因是:苹果构造函数调用了默认的水果构造函数,但找不到

but if I removed apple constructor(commented out from LineA to LineB) and compiled again.但是如果我删除了苹果构造函数(从 LineA 注释到 LineB)并再次编译。 it is compiled without error.它编译没有错误。 why?为什么? from book, if we didn't define any constructor for apple class.从书中,如果我们没有为苹果类定义任何构造函数。 the compiler will create one.编译器将创建一个。 why the apple default constructor created by compiler doesn't complain about missing fruit::fruit() .为什么编译器创建的苹果默认构造函数不会抱怨缺少 Fruit::fruit() 。 thanks谢谢

class fruit
{
    public:
    int seed;
    //fruit()
    //{
    //    cout <<"fruit" <<endl;
    //    seed = 12;
    //}
    fruit(int i)
    {
        cout <<i<<"fruit"<<endl;
    }
    virtual void plant()
    {
        cout << "fruit" <<endl;
    }
};

class apple:public fruit
{
    public:

    apple() //lineA
    {
        //cout << "apple" << endl;
    } //lineB
    void plant()
    {
        cout << "apple" << endl;
    }
};

int main(){}

thanks谢谢

I know how to make this code work.我知道如何使这段代码工作。 my question is why removing the apple default constructor doesnt cause compilation error.我的问题是为什么删除苹果默认构造函数不会导致编译错误。

[ C++11 standard.] [C++11 标准。]

12.1.5. 12.1.5. If there is no user-declared constructor for class X, a constructor having no parameters is implicitly declared as defaulted (8.4).如果类 X 没有用户声明的构造函数,则没有参数的构造函数被隐式声明为默认值 (8.4)。

12..1 ..."The implementation will implicitly define them if they are odr-used"... 12..1 ...“如果它们被 odr 使用,实现将隐式定义它们”...

So, if you remove apple::apple() , the implementation may not create apple::apple() unless it is actually called, and hence has no need to reference fruit::fruit() .因此,如果您删除apple::apple() ,则实现可能不会创建apple::apple()除非它被实际调用,因此不需要引用fruit::fruit()

As given, the code does not call apple::apple() .正如给定的,代码没有调用apple::apple()

You need to have default constructor for fruit or setup an id from apple.您需要为水果设置默认构造函数或从苹果设置一个 id。 So either所以要么

fruit::fruit() {}

or或者

apple::apple() : fruit(1) {}

from book, if we didn't define any constructor for apple class.从书中,如果我们没有为苹果类定义任何构造函数。 the compiler will create one.编译器将创建一个。 why the constructor created by compiler doesn't complain about missing fruit()为什么编译器创建的构造函数不会抱怨缺少水果()

Because you have explicitly declared a constructor.因为您已经显式声明了一个构造函数。

I think what you need is an initialization list.我认为你需要的是一个初始化列表。 You need to explicitly call您需要明确调用

class apple:public fruit
{
    public:

    apple() : fruit(10)
    {
        //cout << "apple" << endl;
    }
    void plant()
    {
       cout << "apple" << endl;
    }

}; };

See http://www.cprogramming.com/tutorial/initialization-lists-c++.html for more details.有关更多详细信息,请参阅http://www.cprogramming.com/tutorial/initialization-lists-c++.html

If you explicitly define a constructor, you are overriding the default one.如果您明确定义了一个构造函数,那么您将覆盖默认的构造函数。 In this case you defined one with one parameter in fruit class.在这种情况下,您在水果类中定义了一个带有一个参数的参数。 Now, when you create an object of class apple, the way inheritance behaves is the following: 1. call the constructor of the base class (fruit class).现在,当您创建类 apple 的对象时,继承的行为方式如下: 1. 调用基类(fruit 类)的构造函数。 In this case it will call default constructor of the base class since you are not explicitly calling the constructor with one argument of fruit 2. execute constructor of derived class ie apple class在这种情况下,它将调用基类的默认构造函数,因为您没有使用水果的一个参数显式调用构造函数 2. 执行派生类的构造函数,即苹果类

The compiler will not be able to find the default constructor of base class (in step 1), hence this error.编译器将无法找到基类的默认构造函数(在步骤 1 中),因此出现此错误。 Solution is to either call constructor of fruit class with one argument in apple class or to define a constructor with no arguments or remove both, whatever suits your needs.解决方案是在苹果类中使用一个参数调用水果类的构造函数,或者定义一个不带参数的构造函数或删除两者,无论您需要什么。

Let us know if you need help with the code or more explanation (code not included with an intention that you will try to understand the OOP concepts).如果您需要代码方面的帮助或更多解释(不包含代码的目的是为了让您尝试理解 OOP 概念),请告诉我们。

The first thing is, the compiler needs to insert code to call the default constructors of base classes and hence it requires a default constructor of base class otherwise throws an error: "no matching function for call ".首先,编译器需要插入代码来调用基类的默认构造函数,因此它需要基类的默认构造函数,否则会抛出错误:“没有匹配的调用函数”。

Now coming to the question that why it does not throw an error if apple() constructor is removed and compiled again because as per concept compiler automatically creates a default constructor if not user defined.现在来回答一个问题,如果 apple() 构造函数被删除并再次编译,为什么它不会抛出错误,因为根据概念,如果不是用户定义的,编译器会自动创建一个默认构造函数。

  • Answer is, just to avoid this error of "no matching function to call", the compiler implicitly deletes the constructor of derived class apple().答案是,为了避免“没有匹配的函数调用”这个错误,编译器隐式删除了派生类apple()的构造函数。 You can prove this by not defining the default constructor of derived class and then creating object of derived class in the main function.您可以通过不定义派生类的默认构造函数,然后在主函数中创建派生类的对象来证明这一点。 It will throw an error like:它会抛出如下错误:

In function 'int main()':在函数“int main()”中:

error: use of deleted function 'apple::apple()'错误:使用已删除的函数“apple::apple()”

note: 'apple::apple()' is implicitly deleted because the default definition would be ill-formed注意: 'apple::apple()' 被隐式删除,因为默认定义格式错误

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM