Tag: copy constructor

复制构造函数和赋值运算符

如果我重写operator=复制构造函数会自动使用新的操作符? 同样,如果我定义了一个拷贝构造函数,那么operator=自动“inheritance”拷贝构造函数的行为吗?

为什么这段代码试图调用复制构造函数?

我只是在Visual Studio中花费了大量的时间来处理错误。 我已经将代码提炼成了下面这个小的可编译的例子,并在IdeOne上试了一下,得到了同样的错误,你可以在这里看到。 我想知道为什么下面的代码尝试调用B(const B&)而不是B(B&&) : #include <iostream> using namespace std; class A { public: A() : data(53) { } A(A&& dying) : data(dying.data) { dying.data = 0; } int data; private: // not implemented, this is a noncopyable class A(const A&); A& operator=(const A&); }; class B : public A { }; int main() […]

为什么用户定义的移动构造函数禁用隐式的复制构造函数?

虽然我正在阅读boost / shared_ptr.hpp,我看到了这样的代码: // generated copy constructor, destructor are fine… #if defined( BOOST_HAS_RVALUE_REFS ) // … except in C++0x, move disables the implicit copy shared_ptr( shared_ptr const & r ): px( r.px ), pn( r.pn ) // never throws { } #endif 什么评论“生成的副本构造函数,析构函数是好的,除了在C + + 11,移动禁用隐式副本”的意思呢? 我们是否总是自己写复制文件来防止这种情况出现在C ++ 11中?