This simple example demonstrates C ++ syntax for invoking base class constructors - as far as I understand it as a C ++ student:
class BaseClass { protected: int i; public: BaseClass(int x) { i = x; } }; class DerivedClass: public BaseClass { int j; public: DerivedClass(int x, int y): BaseClass(y) { j = x; }
Here, the base class constructor can accept named arguments to the derived class constructor as input.
Now, if I want to call the BaseClass()
constructor with an input value that is not a direct input to DerivedClass()
? Basically, I would like to do multi-line work with x
and y
inside DerivedClass()
, and then pass the calculated value to BaseClass()
. Can this be done using constructors? Should this be done using some kind of initialization method?
source share