c++ - Construct object without arguments where superclass requires arguments -
i'm implementing class inherits superclass. superclass requires arguments constructor. subclass instantiatable without requiring arguments. superclass looks this:
class otherclass { public: otherclass(yetanotherclass *yac); }; class superclass { public: superclass(otherclass *c); };
i'd this:
class myclass : public superclass { public: myclass() : superclass(otherclass(yetanotherclass)) {} };
in order avoid having when instantiating member of myclass
:
yetanotherclass * only_used_once = yetanotherclass(); otherclass * also_used_just_once = otherclass(only_used_once); myclass what_i_actually_want = myclass(also_used_just_once);
is possible? a similar question showed solution of creating static method produces arguments needed parent constructor, hope there's simpler way there.
with :
struct dataclass { dataclass() : yetanotherclass(), otherclass(&yetanotherclass) {} yetanotherclass yetanotherclass; otherclass otherclass; };
if each instance of myclass
owns other class may following:
class myclass : private dataclass, public superclass { public: myclass() : dataclass(), superclass(&this->otherclass) {} };
else share other class , may do:
class myclass : public superclass { public: myclass() : superclass(&dataclass.otherclass) {} private: static dataclass dataclass; }; dataclass myclass::dataclass;
Comments
Post a Comment