c++ - Difference in constructors with X() = delete; and private X(); -
this question has answer here:
let assume have class x , want wo explicitly forbid, let standard constructor. used long time in header file:
private: x(); // 1.
so, contructor disabled outside class, anybody. have learned in c++11 follwoing recommended:
x() = delete; // 2.
both achive wish forbid standard contructor.
but exact difference bewteen them? why c++11 recommend last one? there other flags, signals set in 2. way?
example 1 way before had = delete
came out in c++11. have = delete
rid of constructor. making constructor private still use constructor in member function if try default object in member function = delete
compiler error.
#include <iostream> class foo { foo(); public: static void somefunc() { foo f; } }; class bar { public: bar() = delete; static void somefunc() { bar b; } }; int main() { foo::somefunc(); // compile bar::somefunc(); // compiler error }
Comments
Post a Comment