C++ Multiple Inheritance Constructor
c++, constructor
Solution
Since base constructors are called in declaration order, `cInit::cInit()` will be called first. Its constructor would assign `cInit::m_X` member.
Then, `cUse::cUse(cInit *)` will be called and would assign a result of call to `cInit::getX()` to `cUse::m_X`. Given that `cInit::getX()` is not a virtual function, it is safe to call it like that.
In other words, there is nothing wrong with this code. Except that it is ugly (or should I say not well designed?), confusing, and would only cause troubles further down the road.
Hope it helps.
Problem
I need to know something about the constructor. I didn't really know how to phrase the question, but basically I need to have all the action happening in the constructor of the final class, whilst a variable gets created in the constructor of one class and used in the constructor of another. Does this work, and is it safe? Example code below. ``` // Init class class cInit { private: std::string *m_X; public: cInit() { m_X = new std::string; } std::string *getX() { return m_X; } }; // Does this work (?) class cUse { private: std::string *m_X; public: cUse(cInit *x) : m_X( x->getX() ) { } // Final Class - same question here? Does it work? class Final : public cInit, public cUse { public: Final() : cInit(), cUse( this ) { } } ```