c++ - Is it possible to access the current 'to create' obj in constructor? -
i writing tree class , want implement constructor creating tree dimension , depth:
public: tree(); // empty constructor tree(int dimension, int depth); // constructing empty tree // ... void newnode(node<t>* const&, t const&); // ... private: unsigned int mnumnodes; node<t> *mroot; template<typename t> tree<t>::tree(int dimension, int depth): // member vars { // other constructing stuff this->newnode(0, parent); }
apparently not possible, wrote working function adding new nodes specific parent specific value nice use it.
but: method needs access current object.
maybe there kind of solution working me neither pointing obj possible, nor other kind of access.
the problem isn't constructor. @ point execution enters body of constructor, object constructed tree<std::basic_string<char> >
. means can use object instance of class in body of constructor.
the problem using object such in constructor if actual object being constructed derived class of class @ hand. it's knowledge doesn't exist yet in constructor of parent class. it's best not call virtual functions within constructor. it's okay call non-virtual member function within constructor. problem derived class constructors might change things in parent class.
that's not what's happening here. problem here use of 0 node<t>* const&
. won't work, period. if did work, change value of zero.
based on incomplete code have posted, getting rid of ampersand in declaration of first argument newnode
trick.
Comments
Post a Comment