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
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;
}
};
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.
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
)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With