Hi all. Continuing my journey to “upgrade” my programming skills from Good Old C to C++. Got a question about calling a base class’s constructor from the constructor of a derived class. This is the method I’ve seen:
class derivedClass : public baseClass {
public:
derrivedClass(baseParam1, baseParam2, derrivedParam1, derrivedParam2) : baseClass(baseParam1, baseParam2)
{
// derivedClass Constructor body here
}
/*
Rest of derrivedClass definition here
*/
};
My question: is it possible to first do some processing in the body of the derived class’s constructor before calling the base class’s construct? One scenario could be modifying the parameters passed to base class’s constructor for custom limit checking based on the values of the other parameters passed to the derived class’s constructor.
Thanks.
Thanks. So, it appears that the answer is that there is no way to dynamically modify the parameters passed to the base class constructor during the execution of the derived class constructor. Correct?
gfvalvo:
Thanks. So, it appears that the answer is that there is no way to dynamically modify the parameters passed to the base class constructor during the execution of the derived class constructor. Correct?
I'm not really sure what you mean.
As you've read, 1) the base class constructor is called when you instantiate the derived class 2) you can pass parameters to to the base class constructor.
Can you give a more practical example of what you are thinking about doing?
Sure. Say the base class constructor takes some parameters. It may check those parameters against certain limits before applying them. If the parameters are outside those limits, it may modify them.
As in the example I posted, the derived class constructor takes parameters that it passes to the base class constructor as well as parameters it uses itself.
Suppose by nature of the derived class’s operation it wants stricter limits applied to the former parameter set before they’re passed to the base class constructor. Maybe those limits are even functions of the latter set of parameters.
To do this extra limiting, the derived class construct would need to perform some operations (i.e. code) and then pass the modified (limited) parameters to the base class constructor.
Hope I’ve expressed myself clearly. I’ll try to come up with a more concrete example.
gfvalvo:
Thanks. So, it appears that the answer is that there is no way to dynamically modify the parameters passed to the base class constructor during the execution of the derived class constructor. Correct?
Just provide an init() function, or something similar, in the base class that you can call to set the parameters. The base class constructor can call that same init() function so you don't duplicate code.
Regards,
Ray L.
gfvalvo:
I’ll try to come up with a more concrete example.
I want friends, but I want them to be nice guys (I'll befriend a dunce, too).
If someone is a Big Jerk, I've got to change them before I let them be my friend:
enum HumanTypes{
NICE_GUY,
BIG_JERK,
DUNCE,
};
class HumanBeing{
public:
HumanBeing(HumanTypes _type){type = _type;}
HumanTypes type;
private:
};
class MyFriends : public HumanBeing{
public:
MyFriends(HumanTypes tp) : HumanBeing(tp == BIG_JERK? NICE_GUY : tp){}
void getFreindType(void){
switch (type)
{
case NICE_GUY:
Serial.println(F("Nice Guy"));
break;
case BIG_JERK:
Serial.println(F("Big Jerk"));
break;
case DUNCE:
Serial.println(F("Dunce"));
break;
}
}
private:
};
MyFriends tom(NICE_GUY);
MyFriends john(BIG_JERK);
MyFriends billy(DUNCE);
void setup()
{
Serial.begin(9600);
tom.getFreindType();
john.getFreindType();
billy.getFreindType();
}
void loop() {
}
now that he's my friend, john is a nice guy!
MyFriends(HumanTypes tp) : HumanBeing(tp == BIG_JERK? NICE_GUY : tp){}
Got it, thanks.
Presumably a call to a function returning the correct type could also go inside the HumanBeing() invocation?
Or even a lambda function?
gfvalvo:
Or even a lambda function?
MyFriends(HumanTypes tp) : HumanBeing([&tp](){if(tp == BIG_JERK) tp = NICE_GUY; return tp;}()){}
yep