c++ - Most concise way to disable copying class in C++11 -
i have problem dealing deprecated since c++11 default generation of copy constructor , copy assignment operator when there user-defined destructor.
for sufficiently simple classes default-generated constructors, operators , destructor fine. consider following reasons declare destructor:
making trivial destructor virtual in base class:
// header class base1 { public: virtual ~base1() = default; }; class base2 { public: virtual ~base2(); }; // source base2::~base2() = default;
would 4 copy , move special methods generated compiler in these cases? if yes, think fine , there no need complicate
base1
orbase2
.printing debug message in destructor:
// header class d { public: ~d(); }; // source d::~d() { #ifdef debug_this std::cout << "d destructed." << std::endl; #endif }
i believe in case copy constructor , assignment operator generated; move constructor , assignment operator not. want avoid using deprecated default-generating , disable copying of
d
. want avoid floodingd
4deleted
declarations. disabling 1 copy constructor enough? style?
with c++11, clean way follow pattern used in boost (see here)
you create base class copy constructor , copy assignment deleted, , inherit it:
class non_copyable { protected: non_copyable() = default; ~non_copyable() = default; non_copyable(non_copyable const &) = delete; void operator=(non_copyable const &x) = delete; }; class myclass: public non_copyable { ... }
Comments
Post a Comment