C++ Compiler Error C2280 “attempting to reference a deleted function” in Visual Studio 2013 and 2015

From [class.copy]/7, emphasis mine: If the class definition does not explicitly declare a copy constructor, a non-explicit one is declared implicitly. If the class definition declares a move constructor or move assignment operator, the implicitly declared copy constructor is defined as deleted; otherwise, it is defined as defaulted (8.4). The latter case is deprecated if …

Read more

How do I make this C++ object non-copyable?

class Foo { private: Foo(); Foo( const Foo& ); // non construction-copyable Foo& operator=( const Foo& ); // non copyable public: static Foo* create(); } If you’re using boost, you can also inherit from noncopyable : http://www.boost.org/doc/libs/1_41_0/boost/noncopyable.hpp EDIT: C++11 version if you have a compiler supporting this feature: class Foo { private: Foo(); public: Foo( …

Read more

When do we have to use copy constructors?

The copy constructor generated by the compiler does member-wise copying. Sometimes that is not sufficient. For example: class Class { public: Class( const char* str ); ~Class(); private: char* stored; }; Class::Class( const char* str ) { stored = new char[srtlen( str ) + 1 ]; strcpy( stored, str ); } Class::~Class() { delete[] stored; …

Read more

Copy constructor and = operator overload in C++: is a common function possible?

Yes. There are two common options. One – which is generally discouraged – is to call the operator= from the copy constructor explicitly: MyClass(const MyClass& other) { operator=(other); } However, providing a good operator= is a challenge when it comes to dealing with the old state and issues arising from self assignment. Also, all members …

Read more