itemStr it_str = "123456";
这样的代码应该是拷贝初始化?如果是拷贝初始化,那么在C++ Primer 中文版第五版中441页提到
拷贝初始化是依靠拷贝构造函数或移动构造函数来往成的
但是实际调用的却是itemStr(const char *s)
这是为什么,是书的错误还是编译器的优化操作
itemStr
类,如上所写会输出This is normal constructor2.
class itemStr
{
public:
itemStr() :
str()
{
std::cout << "This is default constructor." << std::endl;
}
itemStr(std::string s) :
str(s)
{
std::cout << "This is normal constructor1." << std::endl;
}
itemStr(const char *s) :
str(s)
{
std::cout << "This is normal constructor2." << std::endl;
}
itemStr(const itemStr &it) :
str(it.str)
{
std::cout << "This is copy constructor." << std::endl;
}
itemStr& operator=(const itemStr &it)
{
str = it.str;
std::cout << "This is copy-assignment operator constructor." << std::endl;
return *this;
}
private:
std::string str;
};
There is no mistake in the book. This initialization syntax is copy initialization. The copy/move constructor is not called at runtime because the compiler performs copy elision optimization. The compiler is still allowed to perform this optimization even when the copy/move constructor has visible side effects. The result is that you cannot observe the expected output.
In other words,
itemStr it_str = "123456";
is copy initialization, which does require an available copy/move constructor. The following code will report a compilation error when compiled under gcc/clang.