C++ 显式初始化没有默认构造函数的成员

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31488756/
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 13:57:34  来源:igfitidea点击:

Explicitly initialize member which does not have a default constructor

c++parametersconstructor

提问by Tomaz Fernandes

I′m trying to instantiate an object which has no default constructor so it can be referenced from any methods inside the class. I declared it in my header file, but the compiler says that the constructor for the class creating it must explicitly initialize the member, and I can′t figure out how to do that.

我正在尝试实例化一个没有默认构造函数的对象,以便可以从类中的任何方法中引用它。我在头文件中声明了它,但编译器说创建它的类的构造函数必须显式初始化成员,我不知道如何做到这一点。

Really appreciate your answers, thank you in advance!

非常感谢您的回答,提前谢谢您!

The snippet:

片段:

MyClass.h

我的类.h

include "MyOtherClass.h"

class myClass {

    private:
        MyOtherClass myObject;

    public:
        MyClass();
        ~MyClass();
        void myMethod();

}

MyClass.cpp

我的类.cpp

include "MyClass.h"

MyClass::MyClass() {

   MyOtherClass myObject (60);
   myObject.doSomething();

}

MyClass::myMethod() {

    myObject.doSomething();

}

MyOtherClass.h

MyOtherClass.h

class MyOtherClass {

   private:
      int aNumber;

   public:
      MyOtherClass (int someNumber);
      ~MyOtherClass();
      void doSomething();
}

MyOtherClass.cpp

我的其他类.cpp

include "MyOtherClass.h"

MyOtherClass::MyOtherClass (int someNumber) {
   aNumber = someNumber;
}

void MyOtherClass::doSomething () {
    std::cout << aNumber;
}

回答by Russell Greene

You are almost there. When you create an object in C++, by default it runs the default constructor on all of its objects. You can tell the language which constructor to use by this:

你快到了。在 C++ 中创建对象时,默认情况下它会在其所有对象上运行默认构造函数。您可以通过以下方式告诉语言要使用哪个构造函数:

MyClass::MyClass() : myObject(60){

    myObject.doSomething();

}

That way it doesn't try to find the default constructor and calls which one you want.

这样它就不会尝试查找默认构造函数并调用您想要的构造函数。

回答by Chris Drew

You need to initialize the myObjectmember in the constructor initialization list:

您需要myObject在构造函数初始化列表中初始化该成员:

MyClass::MyClass() : myObject(60) {
   myObject.doSomething();
}

Before you enter the body of the constructor all member variables must be initialized. If you don't specify the member in the constructor initialization list the members will be default constructed. As MyOtherClassdoes not have a default constructor the compiler gives up.

在进入构造函数体之前,必须初始化所有成员变量。如果未在构造函数初始化列表中指定成员,则成员将被默认构造。由于MyOtherClass没有默认构造函数,编译器放弃了。

Note that this line:

请注意这一行:

MyOtherClass myObject (60);

in your constructor is actually creating a local variable that is shadowing your myObjectmember variable. That is probably not what you intended. Some compilers allow you turn on warnings for that.

在您的构造函数中实际上是在创建一个隐藏您的myObject成员变量的局部变量。这可能不是你想要的。某些编译器允许您为此打开警告。

回答by 6502

There are two errors

有两个错误

  1. Your code MyOtherClass myObject(60);is not initializing the member of the class, but it's instead declaring a local variable named myObjectthat will hide the member inside the constructor. To initialize a member object that doesn't have a default constructor you should use member initialization lists instead.

  2. You are trying to learn C++ by experimenting with a compiler.

  1. 您的代码MyOtherClass myObject(60);没有初始化类的成员,而是声明了一个名为的局部变量myObject,该变量将在构造函数中隐藏该成员。要初始化没有默认构造函数的成员对象,您应该改用成员初始化列表。

  2. 您正在尝试通过尝试编译器来学习 C++。

This second error is the most serious error and if not corrected is going to take you to a terribly painful path; the only way to learn C++ is by getting one or two good booksand read them cover to cover. Experimenting with C++ doesn't work well.

第二个错误是最严重的错误,如果不纠正,将带你走上一条非常痛苦的道路;学习 C++ 的唯一方法是获得一两本好书,然后从头到尾阅读它们。用 C++ 试验效果不佳。

No matter how smart you are there's no way you can guess correctly with C++, and in a sense being smart is even dangerous (because you may be tempted to skip over something "you understood already"): the reason is that it happens in quite a few places that the correct C++ way is illogical and consequence of historical evolution of the language.

不管你有多聪明,你都无法用 C++ 正确猜测,从某种意义上说,聪明甚至是危险的(因为你可能会想跳过一些“你已经理解”的东西):原因是它发生在相当一些地方认为正确的 C++ 方式是不合逻辑的,并且是语言历史演变的结果。

In many places C++ is the way it is because of history and not because it makes sense, and no matter how smart you are there's no way you can deduce history... history must be studied.

在很多地方,C++ 之所以如此,是因为历史,而不是因为它有意义,无论你多么聪明,都无法推断出历史……历史必须研究。

回答by kiviak

MyClass::MyClass(): myObject (60){

   myObject.doSomething();

}


Initialization of the data member ends before constructor function body.in the function body you just assign

数据成员的初始化在构造函数体之前结束。在你刚刚分配的函数体中