Suppose I was given an abstract class,
class A
{
public:
A(){};
virtual ~A(){};
virtual float func(const int in1, const int in2, const int in3) = 0;
//
// Some default useful functions
//
private:
//
// Some infrastructure code
//
};
In most of the cases it works just fine
class A1 : public A
{
public:
A1(){};
virtual ~A1(){};
virtual float func(const int in1, const int in2, const int in3)
{
// Do something to in1, in2, in3 to get output
}
};
But lets say for A20 I need to pass an additional argument to make the function work yet I still want to use this abstract class because everything else is similar beside this additional argument. The only way I can think of is passing the parameter through the constructor and store a copy. Is this work around consider as good practice?
class A20 : public A
{
public:
A20(const int in4) : m_in4(in4){};
virtual ~A20(){};
virtual float func(const int in1, const int in2, const int in3)
{
// Do something to in1, in2, in3, m_in4 to get output
}
private:
int m_in4;
};
If not what is the best way to do it? Is it better to duplicate another abstract class with 4 inputs for the function? Or is it better to make A20 independent of A and duplicate "Some default useful functions" and "Some infrastructure code" from A into A20?
EDIT: paddy's comment