Reputation: 474
class EventListener
{
public:
virtual void onEvent (std::string message) = 0;
virtual void onEvent (std::string message, int eventCode) = 0;
};
class CustomEventListener : public EventListener
{
public:
void onEvent(std::string message) {};
void onEvent(std::string message, int eventCode) {}; // I want this to throw an error
};
I want to make it so that overriding one of the overloaded functions excludes the user from overriding the other, potentially throwing a compile-time exception. Is this possible ?
The reason why I want to keep both virtual functions in the interface is to maintain backwards-compatibility with users who already use the first function extensively in their application and I don't want to force them to use the second, newer one.
Making both functions non-pure is not really an option, because I want to force the user to override one of them.
Upvotes: 1
Views: 172
Reputation: 888
As user2079303 answered, you can't do it like you suggested.
However, you may do some walkaround which could do the trick you want to achieve.
class EventListener
{
public:
virtual void onEvent (std::string message)
{
throw std::runtime_error("Deprecated");
}
virtual void onEvent (std::string message, int eventCode)
{
onEvent(message);
}
};
Old classes can still override only the first method.
class CustomEventListener : public EventListener
{
public:
void onEvent(std::string message) {};
};
New classes are forced (thus only by the exception) to override one of them, so they can look like this:
class CustomEventListener : public EventListener
{
public:
void onEvent(std::string message, int eventCode) {};
};
Unfortunately users can also override both of them, but there is nothing you can do about it.
To correctly handle this situation, you should call the second method in your code.
Upvotes: 0
Reputation: 238401
I want to make it so that overriding one of the overloaded functions excludes the user from overriding the other, potentially throwing a compile-time exception. Is this possible ?
No. It's not possible to conditionally prevent overriding of a function depending on other overrides. At least not in standard c++.
The reason why I want to keep both virtual functions in the interface is to maintain backwards-compatibility with users who already use the first function extensively in their application and I don't want to force them to use the second, newer one.
If you add a new pure virtual function to the interface, then all inheriting classes that don't yet implement the new function will become abstract. That will break backwards compatibility.
Preventing the user from implementing one of them would not help. Instead it only prevents them from making a concrete class that inherits the interface.
Upvotes: 2
Reputation: 1
Why not enforce to override only one of them with further implementations:
class CustomEventListener : public EventListener {
public:
virtual void onEvent(std::string message) {};
private: // <<<<<<<<
// I want this to throw an error
virtual void onEvent(std::string message, int eventCode) {
throw std::runtime_error("Deprecated");
}
};
Upvotes: 2