c++ call superclass constructor with calculated arguments

梦想的初衷 提交于 2019-12-12 10:44:25

问题


probably it's super easy, but can someone tell me how I can call the superclass' constructor with arguments calculated in the subclass' constructor? something like this:

class A{
  A(int i, int j);
};

class B : A{
  B(int i);
};

B::B(int i){
  int complex_calculation_a= i*5;
  int complex_calculation_b= i+complex_calculation_a;
  A(complex_calculation_a, complex_calculation_b);
}

//edit: i edited the example so that the superclass takes two arguments which have a relation to each other


回答1:


If you cannot express your calculation in a single-line expression, add a static function, and call it in the way you normally call the constructor of the superclass:

class B : A{
public:
    B(int i) : A(calc(i)) {};
private:
    static int calc(int i) {
        int res = 1;
        while (i) {
            res *= i--;
        }
        return res;
    }
};

EDIT Multiple-argument case:

class B : A{
public:
    B(int i) : A(calc_a(i), calc_b(i)) {};
private:
    static int calc_a(int i) {
        int res = 1;
        while (i) {
            res *= i--;
        }
        return res;
    }
    static int calc_b(int i) {
        int complex_a = calc_a(i);
        return complex_a+10;
    }
};



回答2:


B::B(int i) 
    : A(i * 5) 
{}

With C++11, a more complex way is

B::B(int i)
    : A(([](int x) { return 5 * x; })(i))
{}

For complex cases, a protected init function is more readable.




回答3:


Only like this:

class A{
  A(int i);
};

class B : A{
  B(int i);
};

B::B(int i) : A(i*5) {
}

The call to the parent's constructor can only come in the initialization list. Which means, that whatever you're calculating must be known before B is fully constructed (i.e.: you can't call a B member function, unless its static, but only to rely on the parameters passed to B)




回答4:


struct A
{
    A(int);
};

struct B : public A
{
    B()
        : A(5) // "initialisation list"
    {}
};

You have to do this inside the list, but you can use a function. Edit: If you use a function, you probably want to make it a private static member of B.




回答5:


class A
{
public:
   A(int i){}
};


class B : public A
{
public:
    B(int i):A(i*5){}
};


来源:https://stackoverflow.com/questions/9539550/c-call-superclass-constructor-with-calculated-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!