简体   繁体   English

C++ 成员初始化列表

[英]C++ Member Initialization List

Please explain how to use member initialization lists.请解释如何使用成员初始化列表。 I have a class declared in a .h file and a .cpp file like this:我在.h文件和.cpp文件中声明了一个类,如下所示:

class Example
{
private:
    int m_top;
    const int m_size;
    ...
public:
    Example ( int size, int grow_by = 1 ) : m_size(5), m_top(-1);
    ...
    ~Example();
};

I'm initializing m_size on object creation because of const .由于const我在对象创建时初始化m_size How should I write the constructor?我应该如何编写构造函数? Should I repeat : m_size(5), m_top(-1) , or I can omit this step?我应该重复: m_size(5), m_top(-1) ,还是可以省略这一步?

Example::Example( int size, int grow_by)
{
... some code here
}

or或者

Example::Example( int size, int grow_by) : m_size(5), m_top(-1)
{
... some code here
}

Just to clarify something that came up in some of the other answers...只是为了澄清一些其他答案中出现的问题......

There is no requirement that the initialization list be in either the source (.cpp) or header (.h) file.不要求初始化列表位于源 (.cpp) 或标头 (.h) 文件中。 In fact, the compiler does not distinguish between the two types of files.事实上,编译器并不区分这两种类型的文件。 The important distinction is between the contructor's declaration and it's definition.重要的区别在于构造函数的声明和它的定义之间。 The initialization list goes with the definition, not the declaration.初始化列表与定义一致,而不是声明。
Usually, the declaration is in a header file and the definition is in a source file, however, this is not a requirement of the language (ie it will compile).通常,声明在头文件中,定义在源文件中,但是,这不是语言的要求(即它会编译)。 It is not unusual to provide constructor definitions inline in the class declaration when the constructor is empty or short.当构造函数为空或短时,在类声明中内联提供构造函数定义并不少见。 In that case an initialization list would go inside the class declaration, which would probably be in a header file.在这种情况下,初始化列表将进入类声明,它可能位于头文件中。

MyClass.h我的类.h

class MyClass
{
public:
    MyClass(int value) : m_value(value)
    {}
private:
    int m_value;
};

This is the initialization list :这是初始化列表:

Example::Example( int size, int grow_by) : m_size(5), m_top(-1)
{
... some code here
}

and it should be done only in the cpp file.它应该只在 cpp 文件中完成。

Don't you get an error when you do it like you did in the header in your example?当您像在示例中的标题中那样执行此操作时,您不会收到错误消息吗?

Member Initializer list should be a part of a definition in the source file.成员初始值设定项列表应该是源文件中定义的一部分。
Write this in the .cpp file:在 .cpp 文件中写入:

Example ( int size, int grow_by) : m_size(5), m_top(-1)
{

}

The header file should only have:头文件应该只有:

Example ( int size, int grow_by = 1 );

The header file only declares the constructor, the member initializer list is not a part of the declaration.头文件只声明了构造函数,成员初始化列表不是声明的一部分。

Adding to others answers, the one most important thing one should remember about initialization list is that, the order of initialization is decided in the order in which the data members are declared, not the the order in which you have initialized the data members using initialization list添加到其他答案中,关于初始化列表的最重要的一件事是, the order of initialization is decided in the order in which the data members are declared, not the the order in which you have initialized the data members using initialization list

Consider the example (Yours):考虑这个例子(你的):


class Example
{
private:
    int m_top;
    const int m_size;
    ...
public:
    Example ( int size, int grow_by = 1 ) : m_size(5), m_top(-1){}

                   /* Though size is initialized with a value first
                       But it is m_top variable that is assigned value -1
                       before m_size is assigned value 5 */
    ...
    ~Example(){}
};
If one is not aware of the above it can cause very serious implications. 如果不了解上述内容,可能会导致非常严重的后果。

In C++11 you can use non-static data member initialization .在 C++11 中,您可以使用非静态数据成员初始化 This is especially useful if you have several constructors that need a common value for a member variable.如果您有多个构造函数需要成员变量的公共值,这将特别有用。

class Example
{
private:
    int m_top = -1;
    const int m_size = 5;
    ...
public:
    Example ( int size, int grow_by = 1 );
    ...
    ~Example();
};

...

Example::Example( int size, int grow_by )
{
    ... some code here
}

You can override the value in a constructor if you need to.如果需要,您可以覆盖构造函数中的值。

You cannot have initializing list both in header and in cpp.您不能在标题和 cpp 中都有初始化列表。 The list should be in which ever file defines your constructor.该列表应该在定义您的构造函数的文件中。 Also you must include a body for the constructor even if it's empty.此外,即使它是空的,您也必须为构造函数包含一个主体。

On a side note you should only specify the default arguments in the function prototype not in the definition.附带说明一下,您应该只在函数原型中而不是在定义中指定默认参数。

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

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