如何在c ++中重新初始化基类

yeh*_*ahs 1 c++

我派生了类和基类.在派生类的构造函数中,我必须使用基类的基本构造函数.然后我想用deiffernet基类构造函数重新构造基类:

class A
{
public:
    int a, b;
}

class B : public A
{
    B() : A()
    {
...
    //do some calculations to calculate a and b and then 
   //re-construct class A with the right values.
       A(a,b) <--- ????
    }

}
Run Code Online (Sandbox Code Playgroud)

我该怎么做?

Hak*_*rce 6

构造函数旨在创建对象.因此它们被使用一次.您应该创建一个方法来进行初始化并从构造函数中调用它.