C++拷贝构造函数:C++ Primer
PHP中文网
PHP中文网 2017-04-17 15:38:26
0
1
537
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;
};
PHP中文网
PHP中文网

认证0级讲师

reply all(1)
黄舟

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.

class A {
 public:
  A(const char *) {}

  A(const A &) = delete;
  A(A &&) = delete;
};

int main() {
  A x = "abc";
  return 0;
}

main.cc: In function ‘int main()’:
main.cc:12:9: error: use of deleted function ‘A::A(A&&)’
   A x = "abc";
         ^
main.cc:8:3: note: declared here
   A(A &&) = delete;
   ^
main.cc:5:3: note:   after user-defined conversion: A::A(const char*)
   A(const char *) {}
   ^
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!