Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ call superclass constructor with calculated arguments

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

like image 505
Mat Avatar asked Mar 02 '12 19:03

Mat


4 Answers

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;
    }
};
like image 130
Sergey Kalinichenko Avatar answered Nov 02 '22 23:11

Sergey Kalinichenko


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.

like image 43
Alexandre C. Avatar answered Nov 02 '22 22:11

Alexandre C.


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)

like image 36
littleadv Avatar answered Nov 02 '22 23:11

littleadv


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.

like image 31
cooky451 Avatar answered Nov 02 '22 23:11

cooky451