I am dealing with a cyclic dependency problem in C++.
The situation looks like the following:
libA.so:
- Body.cpp
- Header.cpp
- DataObject.cpp
- DataObject::read(boost::asio::streambuf* data)
{
boost::asio::streambuf data;
....
body = (new DataConverter<Body>)->convert(&data);
header = (new DataConverter<Header>)->convert(&data);
}
libB.so:
- DataConverter.cpp
-> DataConverter<T>
-> T* DataConverter<T>::convert(boost::asio::streambuf* data)
libA.so <-> libB.so
There is a cyclic dependency because libA uses the Converter-Class from libB and libB needs to now about the Object-Types of libA that need to be converted - since DataConverter::convert returns a Body or Header object.
I thought about solving this issue with forward declarations - but that does not seem to be the cleanest solution to me. All in all my plan was to provide an extensible DataConverter solution.
What would you guys suggest as best practice? A complete different design is also welcome :)
Best, Sebastian
You might want to create abstract base classes defining the interface and hide the implementations (derived classes) from each other.