First of all, I think this problem has already been discussed and is pretty common but id doesn't seem to find an answer appropriate to my problem. My problem is related to circular dependencies, which by itself can be solved by using forward declaration, however, in my case, I need to know more then the class name in the forward declaration (like attributes and functions).
In my code both classes, Time and Distance/Speed need to know each other and each other attributes. I avoided the problem by declaring an "Interface" of Time which contains the attribute that is needed for Distance/Speed, but I'm wondering if there is a more elegant solution to this problem that wouldn't involve creating an "Interface".
class ITime
{
public:
ITime(float s) :time_s(s) {};
float time_s;
};
class Speed
{
public:
Speed(float ms = 0) :speed_ms(ms) {}
float speed_ms;
};
class Distance
{
public:
Distance(float m = 0) :distance_m(m) {}
float distance_m;
Speed operator/ (const ITime& t) const
{
return Speed(distance_m / t.time_s);
};
};
class Time :public ITime
{
public:
Time(float s):ITime(s) {}
Distance operator *(const Speed& speed) const
{
return Distance(time_s * speed.speed_ms);
}
};