C++ 是否需要在头文件中定义初始化列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15335193/
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
Is it required to define the initialization list in a header file?
提问by E235
Recently I created class Square
:
最近我创建了类Square
:
=========header file======
========头文件======
class Square
{
int m_row;
int m_col;
public:
Square(int row, int col): m_row(row), m_col(col)
};
==========cpp file======
==========cpp文件======
#include "Square.h"
Square::Square(int row, int col)
{
cout << "TEST";
}
but then I receive lots of errors. If I remove the cpp file and change the header file to:
但后来我收到了很多错误。如果我删除 cpp 文件并将头文件更改为:
=========header file======
========头文件======
class Square
{
int m_row;
int m_col;
public:
Square(int row, int col): m_row(row), m_col(col) {};
};
it complies with no errors. Does it mean that initialization list must appear in the header file?
它符合没有错误。这是否意味着初始化列表必须出现在头文件中?
采纳答案by uba
You can have
你可以有
==============header file ================
==============头文件================
class Square
{
int m_row;
int m_col;
public:
Square(int row, int col);
};
==================cpp ====================
==================cpp ====================
Square::Square(int row, int col):m_row(row), m_col(col)
{}
回答by LihO
Initialization listis part of constructor's definitionso you need to define it at the same place you define constructor's body. This means that you can have it either in your header file:
初始化列表是构造函数定义的一部分,因此您需要在定义构造函数主体的同一位置定义它。这意味着您可以在头文件中使用它:
public:
Square(int row, int col): m_row(row), m_col(col) {};
or in .cpp file:
或在 .cpp 文件中:
Square::Square(int row, int col) : m_row(row), m_col(col)
{
// ...
}
but when you have definition in .cpp file, then in header file, there should be only its declaration:
但是当你在 .cpp 文件中有定义时,那么在头文件中,应该只有它的声明:
public:
Square(int row, int col);
回答by Steve Jessop
The initialization list appears with the constructor definition, not with a declaration that isn't a definition. So, your options are either:
初始化列表与构造函数定义一起出现,而不是与不是定义的声明一起出现。因此,您的选择是:
Square(int row, int col): m_row(row), m_col(col) {}; // ctor definition
in the class definition or else:
在类定义中,否则:
Square(int row, int col); // ctor declaration
in the class definition and:
在类定义和:
Square::Square(int row, int col): m_row(row), m_col(col) {} // ctor definition
elsewhere. "Elsewhere" is allowed to be in the header, if you make it inline
.
别处。“其他地方”允许出现在标题中,如果你做到了inline
。
回答by Mahesh
Not a requirement. It can be implemented in a source file as well.
不是要求。它也可以在源文件中实现。
// In a source file
Square::Square(int row, int col): m_row(row),
m_col(col)
{}
回答by Arul Kumar
This kind of initializing a variable called member initialization list. Member initialization list can be used in header file or source file. That doesn't matter. But the constructor must have definition when you initialize it in header file. You can refer C++ Member Initialization Listfor more details.
这种初始化变量叫做成员初始化列表。成员初始化列表可用于头文件或源文件。那没关系。但是构造函数在头文件中初始化时必须有定义。您可以参考C++ 成员初始化列表以获取更多详细信息。