Mat
Mat

Reputation: 4501

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

Upvotes: 6

Views: 2870

Answers (5)

user1200540
user1200540

Reputation:

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


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

Upvotes: 0

Alexandre C.
Alexandre C.

Reputation: 56956

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.

Upvotes: 3

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726499

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;
    }
};

Upvotes: 7

cooky451
cooky451

Reputation: 3510

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.

Upvotes: 1

littleadv
littleadv

Reputation: 20272

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)

Upvotes: 1

Related Questions