简体   繁体   English

C ++多重继承和模板

[英]C++ Multiple inheritance and templates

As we all know, C++ allows multiple inheritance. 众所周知,C ++允许多重继承。

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. 我正在实现一个处理网络,其中一些处理节点之间相互链接,以便用一种经过修改的Observer模式交换不同的数据。

A node which can send a certain type of data is a "DataSender" and then extends this abstract class. 可以发送特定类型数据的节点是“DataSender”,然后扩展此抽象类。

A node which can receive a certain type of data is a "DataReceiver" and then extends this abstract class. 可以接收某种类型数据的节点是“DataReceiver”,然后扩展该抽象类。

Here is my piece of code : 这是我的一段代码:

DataReceiver.h 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 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: 我想要一个发送“Image”和“Text”类型数据的节点:然后我有一个节点: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). 因为它无法区分哪个sendData()与父类(来自继承)应该使用(这是多重继承的常见问题)。

  • 1) Is there a way to use sendData() with something to solve the ambiguity (i am not sure there is one ? 1)有没有办法使用sendData()来解决模糊性(我不确定有一个?

  • 2) Is there another way to solve my problem of communication ? 2)有没有其他方法可以解决我的沟通问题? (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. 我不认为你有一个歧义问题,因为两个sendData成员函数采用不同的参数。 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 . 问题更可能是由于确定调用哪个函数C ++以特定顺序检查基类,但在具有正确名称的成员函数的第一个函数中停止。 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: 您可以采取的措施是将以下行添加到Node类定义中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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