How to write an interface but with different signatures
Chris Katko
ckatko at gmail.com
Sun Nov 19 08:13:16 UTC 2023
I know that sounds stupid on the face of it. An interface
shouldn't change. But consider this:
A frame timer and a clock timer(seconds) that re-use
functionality from a parent class/interface.
```
class timerType
{
void start() = 0;
void stop() = 0;
void restart() = 0;
void set(?) = 0; // ????
}
class frameTimer : timerType
{
void set(int numFrames){}
}
class clockTimer : timerType
{
void set(float numSeconds){}
}
```
If we put both signatures in the top we're kind of polluting the
interface. If we don't put them in the interface, then you can
create a timer with no set() function.
None of this is super important on a practical level. There's
going to probably be a total of two or three timer types. But as
a learning exercise, I'm curious if there is a right/proper/best
way to solve this conceptual problem.
And to be clear, frames and seconds are NOT directly
interchangeable or convertible. If the game runs at 2 FPS for a
moment, a 5 second timer is still 5 seconds (e.g. a countdown),
but a frame timer of 2 (for an animation) is still going to be 2
frames.
More information about the Digitalmars-d-learn
mailing list