There are two classes

class cFunction { public: cFunction(double _a, double _b, double _c, double _x); virtual double getY(); protected: virtual double function(); cArguments *args; double x, y; }; class cFunction1 : public cFunction { public: cFunction1(double _a, double _b, double _c, double _x); protected: double function(); }; 

the constructor for the parent looks like

 cFunction::cFunction(double _a, double _b, double _c, double _x) { args = new cArguments(_a,_b,_c); x = _x; y = function(); } 

if for a successor, the constructor is done as described below, then the function () method is called parent, despite the fact that it is virtual

 cFunction1::cFunction1(double _a, double _b, double _c, double _x) : cFunction(_a, _b, _c, _x) { } 

if you do this

 cFunction1::cFunction1(double _a, double _b, double _c, double _x) { args = new cArguments(_a,_b,_c); x = _x; y = function(); } 

then there is an error

 In constructor 'cFunction1::cFunction1(double, double, double, double)': no matching function for call to 'cFunction::cFunction()' 

How to do, what would the constructor of the heir use its own function method?

    2 answers 2

    The base constructor always calls the functions of the base class.

    Concerning the second error. You do not explicitly call the base class constructor, and the compiler tries to call the default constructor, but it does not, because you have overloaded it. Here's the compiler and swears that he can not find it.

      if for a successor, the constructor is done as described below, then the function () method is called parent, despite the fact that it is virtual

      In the constructor of the descendant, the virtual table is overwritten by the most recent action, after the initialization of all fields of the ancestors, therefore polymorphism in the constructor does not work.

      How to do, what would the constructor of the heir use its own function method?

      Move initialization beyond the constructor.

      Update: The function , of course, can be redefined by inserting an intermediate class into the inheritance chain, in which to override function , but this does not solve the problem, since The first ancestor that uses function is cFunction , i.e. the class that spawns the polymorphic chain function and at the same time uses it.

      Now, if you separate them (that is, in one class, define function , and in the other, the constructor that uses it), then you can turn this focus. But, in this case, you still have to override the function in the intermediate class , substituting it in the inheritance chain before cFunction .

      • See the answer update. - mega