How do I allow two classes mutually include one another so they can convert from one to the other.
Car.hpp
#ifndef CAR_HPP
#define CAR_HPP
#include "Truck.hpp"
class Car
{
public:
Car(int weight) : weight(weight) {}
Car(Truck data) : weight(ConvertFromTruck(data)) {}
private:
int weight;
int ConvertFromTruck(Truck data)
{
... //in real life there would be a lot more to transfer than just weight.
}
}
#endif //CAR_HPP
Truck.hpp
#ifndef TRUCK_HPP
#define TRUCK_HPP
#include "Car.hpp" //Obviously won't be included because of the CAR_HPP include guard
class Truck
{
public:
Truck(int weight) : weight(weight) {}
Truck(Car data) : weight(ConvertFromCar(data)) {}
private:
int weight;
int ConvertFromCar(Car data)
{
...//in real life there would be a lot more than just weight
}
}
#endif //TRUCK_HPP
Main.cpp
#include "Car.hpp"
#include "Truck.hpp"
int main()
{
Car newCar(42);
Truck newTruck(newCar);
return 0;
}
So obviously Truck.hpp can't truly include Car.hpp becuase CAR_HPP is already defined. Also, Truck.hpp can not forward declare class Car;
because Truck(Car data)...
requires a complete type, and a forward declared class is not a complete type.
It looks like this is similar: Forward declaration being ignored? but there is no answer.
This topic states not to have mutual including headers. Forward Declarations and Includes
I would try to avoid this but how can I achieve a Car that can receive a Truck and convert it properly and a Truck that can receive a Car and convert it properly?
Is there a way I can use:
operator Car() { ... }
and operator Truck() { ... }
so that a Car can be casted into a Truck and vice versa?