Saturday, 15 September 2012

c++ - Public deleted or private default ctor/assignment/copy ctor? -



c++ - Public deleted or private default ctor/assignment/copy ctor? -

if want forbid re-create construction/assignment is:

class foo { public: foo(const foo&) = delete; foo& operator = (const foo&) = delete; };

the same as:

class foo { private: foo(const foo&) = default; foo& operator = (const foo&) = default; };

which right way , why?

the right way first solution : re-create constructor , assignment operators not defined, effort utilize them not compile.

class foo { public: foo(const foo&) = delete; foo& operator = (const foo&) = delete; };

the sec declaring and defining implicitly generated forms private :

an object of type foo allowed re-create itself. any friend class or method allowed re-create foo

so re-create construction/assignment still possible.

you utilize boost::noncopyable base of operations class, c++11 (see source code here)

c++ c++11 default-constructor

No comments:

Post a Comment