简体   繁体   中英

C++ Multiple inheritance and templates

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,

它不漂亮,但你可以告诉你打算调用哪个基类的功能

node->DataSender<Text>::sendData(<my argument>);

I don't think you have an ambiguity problem, because the two sendData member functions take different arguments. The problem is more likely caused by the fact that when determining which function to call C++ checks base classes in a specific order, but stops in the first one that has a member function of the correct name . Then, if it has found one that can take the argument you supplied it calls it, otherwise it issues the error you probably saw.

What you can do to overcome this problem is to add the following lines to your Node class definition:

using DataSender<Image>::sendData;
using DataSender<Text>::sendData;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM