如果我使用显式构造函数,是否需要将关键字放在 .h 和 .cpp 文件中?
其实我的问题都在标题里。
无论如何:
我有一个类,我使用显式构造函数:
.h
class MyClass
{
public:
explicit MyClass(const string& s): query(s) {}
private:
string query;
}
是否必须在实现(.cpp)文件中放置显式关键字?
Actually my question is all in the title.
Anyway:
I have a class and I use explicit constructor:
.h
class MyClass
{
public:
explicit MyClass(const string& s): query(s) {}
private:
string query;
}
Is it obligatory or not to put explicit keyword in implementation(.cpp) file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关于后续问题(您确实应该作为单独的问题提交),初始化列表与构造函数的实现(其函数体)一起使用,该实现可能位于标头或 cpp 文件中。
Re the followup question (which you really should have submitted as a separate question), the initialization list goes with the constructor's implementation (its function body), which might be in either the header or the cpp file.
不它不是。 仅允许在标头中使用
explicit
关键字。 我的 gcc 说:对于以下代码:
No, it is not. The
explicit
keyword is only permitted in the header. My gcc says:for the following code: