c++ - Init. static members while COPY CTOR is private -
i have class x, , goal have special var indicates "bad object", in order implement function returns x&
.
example:
//x.h class x{ private: int i; x(const x& other){} //private copy ctor public: const static x& badobject; x(int a) : i(a) {} // ctor const x& f(){ if(true) //return valid x object else return badobject; };
the ctor not default ctor, , copy ctor private (i don't want allow coping of object.) operator=
private.
now, when try init. badobject in x.cpp error:
//x.cpp #include "x.h" const x& x::badobject = x(1);
because copy ctor private.
what doing wrong here? should resolve this?
thanks!
change declaration to
const static x badobject;
and definition to
const x x::badobject(1);
this create 1 instance of badobject
. function returns value ref, there no need copy constructor or more.
Comments
Post a Comment