如果我使用显式构造函数,是否需要将关键字放在.h和.cpp文件中
If I use explicit constructor, do I need to put the keyword in both .h and .cpp files?
实际上我的问题都在标题中
无论如何:
我有一个类,我使用显式构造函数:
.h
class MyClass
{
public:
explicit MyClass(const string& s): query(s) {}
private:
string query;
}
是否必须在实现(.cpp)文件中放入显式关键字?
不,不是。explicit
关键字只允许在标头中使用。我的gcc说:
test.cpp:6: error: only declarations of constructors can be 'explicit'
对于以下代码:
class foo {
public:
explicit foo(int);
};
explicit foo::foo(int) {}
关于后续问题(您确实应该将其作为单独的问题提交),初始化列表与构造函数的实现(其函数体)一起提供,该实现可能在头文件或cpp文件中。
相关文章:
- extern 关键字在包含文件头文件C++
- 读取文本文件,其中C++读取特定关键字后面的值
- 使用关键字从文本文件返回数字
- 在特定字段中关键字搜索后,从文本文件中提取多个记录
- 如果我在标题文件中使用关键字的声明,则应该在定义本身中包含相同的关键字
- 如何在.cpp文件中使用C++关键字
- C++:定义类函数的.cpp文件中实例本身的表示关键字
- 如何在文件中搜索关键字,然后打印其中包含的字符串
- 具有多个文件的extern关键字
- 在二进制格式的xml文件中查找关键字
- 何时在带有头文件和目标文件的类方法中使用“inline”关键字
- 在 VC++ 中访问一个文件中声明的变量.(外部关键字用法)
- 使用关键字解析 C++ 中的文件
- C++ 在关键字段上联接两个管道分隔文件
- C++,逐行读取并搜索链接到文件中某行的关键字
- 不能对 c++ 类实现文件中的静态方法使用 "static" 关键字 (.cpp)
- 如果在头文件中定义方法,内联关键字有什么用?
- 这里需要extern关键字,cpp文件中的const
- 头文件与源/实现文件中关键字包含C++ "Rules"
- 当我们在该类的头文件中定义一个类成员函数时,必须使用内联关键字.为什么