As we all know, C++ allows multiple inheritance.
Context
I’m implementing a processing network where some processing nodes are link between each other to exchange different data with a sort of modified Observer pattern.
A node which can send a certain type of data is a “DataSender” and then extends this abstract class.
A node which can receive a certain type of data is a “DataReceiver” and then extends this abstract class.
Here is my piece of code :
DataReceiver.h
template <typename TReceivedData>
class DataReceiver {
public:
void receiveData(TReceivedData* receivedData)
{
m_receivedData = receivedData;
}
TReceivedData* getReceivedData()
{
return(m_receivedData);
}
private:
TReceivedData* m_receivedData;
DataSender.h
template <typename TSentData>
class DataSender {
public:
void sendData(TSentData* sentData)
{
set<DataReceiver<TSentData>*>::const_iterator it;
for(it = m_receiverList.begin(); it != m_receiverList.end(); ++it)
(*it)->receiveData(sentData);
}
void addDataReceiver(DataReceiver<TSentData>* dataReceiver)
{
m_receiverList.insert(dataReceiver);
}
void removeDataReceiver(DataReceiver<TSentData>* dataReceiver)
{
m_receiverList.erase(dataReceiver);
}
private:
set<DataReceiver<TSentData>*> m_receiverList;
};
Then a new node is simply implemented by extending one or both of these abstract classes.
Question
I want a node which sends a data of type “Image” and “Text” : then I have a node :
with:
class Node : public DataSender<Image>, DataSender<Text>
Well, i guess you’ve already seen my problem, the compilation won’t allow this as there’s an ambiguity if I launch :
Node* node;
node->sendData(<my argument>);
because it has no way to distinguish which sendData() from the parents classes (from inheritance) should be used (that’s a common problem of multiple inheritance).
-
1) Is there a way to use sendData() with something to solve the ambiguity (i am not sure there is one ?
-
2) Is there another way to solve my problem of communication ? (I absolutely want to have the opportunity that the final user which wants to create a node which sends/receives data can do it easily simply by extending something like an interface, and datas should be on different “channels”: a node for instance could be able to process text and image, but will only send image…
Thanks for your help,
Julien,
It’s not pretty, but you can tell which base class’ function you intend to call