Reputation: 613
I have an abstract class A
and a pure-virtual function toDoOrNotToDo
in it. I have two subclasses, A1
and A2
, such that A1
needs the const int x
parameter of toDoOrNotToDo
to do its job, and A2
doesn't need that parameter to do it's job.
But if I write
class A2 : public A {
public:
int toDoOrNotToDo() const override;
};
It will obviously complain:
‘int A2::toDoOrNotToDo() const’ marked override, but does not override
I can just write:
class A {
public:
virtual int toDoOrNotToDo(const int x) const = 0;
virtual ~A();
};
class A1 : public A {
public:
int toDoOrNotToDo(const int) const override;
};
class A2 : public A {
public:
int toDoOrNotToDo(const int) const override;
};
But then in A2::toDoOrNotToDo
I have a parameter that I don't need.
How can I improve the design?
Upvotes: 1
Views: 84
Reputation: 171127
Decide on the interface first. If the int
parameter is not supposed to be needed for the function, then an implementation cannot require it without violating the Liskov Substitution Principle.
If it is supposed to be needed, just put it into the interface class. As long as the parameter is documented as "may not affect the result," all is well.
However, the whole situtation looks like a bad design to me—maybe the abstraction is not really valid and you're using the wrong tool for the job?
Upvotes: 4