In a current project, I have classes named BlahManager and class named BlahController, defined like this:
class BlahController
{
public:
void doSomething(int x, float y){
// do the work
}
void doOtherThing(char a, char* b, int c){
// do the work
}
}
class BlahManager
{
public:
BlahController m_controllers[5];
void doSomething(int blahIndex, int x, float y){
m_controllers[blahIndex].doSomething(x,y);
}
void doOtherThing(int blahIndex, char a, char* b, int c){
m_controllers[blahIndex].doOtherThing(a,b,c);
}
}
Is their anyway to make abstract class which defines functions doSomething and doOtherthing only once, and make both class Manager and Controller implement it?
I want to do this to force the similarity of function names/parameters between the Manager and the Controller.
No and you shouldn’t.
BlahController::doSomethingtakes 2 parameters, whileBlahManager::doSomethingtakes 3. So you cannot have an abstract parent class forcing the two classes to implement the same function, because they are not the same function.Also, you shouldn’t even want to do this. These functions don’t do similar jobs. For example if you have
drawforclass Circleandclass Triangle, they are doing similar jobs. But here, one is performing a job, while the other is managing it. So, even if the function signatures were similar, I would never make them inherit it from a common parent!