C++ 从模板类继承问题

C++ 从模板类继承问题,c++,templates,inheritance,C++,Templates,Inheritance,嗨,我想做这样的事: template<typename T> struct APP_Interface{ APP_Interface* shared; Mutex m; T data; virtual void publish(){ //copy data from this->data to shared->data } virtual void receive(){ //copy data fro

嗨,我想做这样的事:

template<typename T>
struct APP_Interface{
    APP_Interface* shared;
    Mutex m; 
    T data;

    virtual void publish(){
    //copy data from this->data to shared->data
    }

    virtual void receive(){
    //copy data from shared->data to this->data 
    }
};

struct MyInterface : APP_Interface<MyInterface>{
    float MyData1;
    float MyData2;
};
struct MyInterface : ??? {
    float MyData1;
    float MyData2;
};
那么,当我应该使用它时,我只想:

//Thread1.hpp
class thread1{
    void run(){
        interface.MyData1 = 100; 
        interface.publish(); 
    }

public:
    MyInterface interface;
}

//And something similar for thread2 at the receiving end
最后,我想通过让main.c创建两者之间的链接并分配共享结构来实现一些“依赖注入”的功能。例如:

//main.cpp
void main(){
    //Bind interfaces:
    MyInterface interface({ &thread1.interface,  //PROVIDER
                            &thread2.interface,  //CONSUMER
                          });
} 
我不知道这个解释是否有意义:)

  • 数据
    声明为指针
  • 提供getter和setter函数。在这些函数中,确保数据是从堆中分配的
  • 这是最新的课程

    template<typename T>
    struct APP_Interface{
        APP_Interface* shared;
        Mutex m; 
        T* data;
    
        APP_Interface : data(nullptr) {}
    
        T const& getData() const
        {
           if (!data)
           {
              data = new T;
           }
           return *data;
        }
    
        void setData(T const& newData)
        {
           if (!data)
           {
              data = new T;
           }
           *data = newData;
        }
    
        virtual void publish(){
        //copy data from this->data to shared->data
        }
    
        virtual void receive(){
        //copy data from shared->data to this->data 
        }
    };
    
    模板
    结构应用程序接口{
    应用程序接口*共享;
    互斥m;
    T*数据;
    APP_接口:数据(nullptr){
    T常量&getData()常量
    {
    如果(!数据)
    {
    数据=新T;
    }
    返回*数据;
    }
    void setData(T const和newData)
    {
    如果(!数据)
    {
    数据=新T;
    }
    *数据=新数据;
    }
    虚拟无效发布(){
    //将数据从此->数据复制到共享->数据
    }
    虚拟空接收(){
    //将数据从共享->数据复制到此->数据
    }
    };
    
    这毫无意义。你想达到什么目标?通常,您要么希望将派生类模板化,要么希望专门化APP_接口。我猜您希望这样:'struct MyData{float MyData1;float MyData2;};应用程序接口myDataPublisher;'显然,这会失败。你会有一个MyInterface,它是一个包含MyInterface的应用程序接口,它是一个包含。。。我认为我们无法回答如何解决这个问题,除非你告诉我们你打算如何使用它。就我个人而言,我会出版和接收摘要,并完成它。
    T数据应该是
    T*数据无论如何,请看,如何正确创建和使用
    CRTP
    。我正在尝试避免动态内存^^。@MadScienceDreams检查更新。我正在尝试避免动态内存分配:)在这种情况下,您必须为您的类找到不同的设计。您不能使用CRTP模式。
    template<typename T>
    struct APP_Interface{
        APP_Interface* shared;
        Mutex m; 
        T* data;
    
        APP_Interface : data(nullptr) {}
    
        T const& getData() const
        {
           if (!data)
           {
              data = new T;
           }
           return *data;
        }
    
        void setData(T const& newData)
        {
           if (!data)
           {
              data = new T;
           }
           *data = newData;
        }
    
        virtual void publish(){
        //copy data from this->data to shared->data
        }
    
        virtual void receive(){
        //copy data from shared->data to this->data 
        }
    };