c++ - 检测继承类并将其转换为基类

标签 c++

我有3个类,一个是基类,另一个是基类的继承类,下面是类的代码:

// Event Class
#ifndef EVENT_H
#define EVENT_H

#include <iostream>

namespace Engine
{
    namespace Data
    {
        // base class
        class Event
        {
            public:
                // Class Variable
                int Measure;
                int Beat;
                int Position;

                // This Class that was I mean
                class SampleEvent;
                class TimeEvent;

                // Constructor
                Event(int measure, int beat, int pos);
        };

        // Sample Event Class (inherit to Event Class)
        class Event::SampleEvent : public Event
        {
            public:
            // variable in SampleEvent Class
            int ID;
            float Pan;
            float Vol;

            // Constructor
            SampleEvent(int id, float pan, float vol, int measure, int beat, int pos);
        };

        // Time Event Class (inherit to Event class)
        class Event::TimeEvent : public Event
        {
            public:
            // variable in TimeEvent Class
            double Value;

            // Constructor
            TimeEvent(double value, int measure, int beat, int pos);
        };

        // Constructor of Event
        Event::Event(int measure, int beat, int pos)
        {
            Measure         = measure;
            Beat            = beat;
            Position        = pos;
        }

        // Constructor of Sample Event
        Event::SampleEvent::SampleEvent(int id, float pan, float vol, int measure, int beat, int pos) : Event(measure, beat, pos)
        {
            ID                      = id;
            Pan                     = pan;
            Vol                     = vol;
            Measure         = measure;
            Beat            = beat;
            Position        = pos;
        }

        // Constructor of Time Event
        Event::TimeEvent::TimeEvent(double value, int measure, int beat, int pos) : Event(measure, beat, pos)
        {
            Value                   = value;
            Measure         = measure;
            Beat            = beat;
            Position        = pos;
        }
    }      
}
#endif

比方说,我有 2 个变量,SETESE 用于 SampleEvent,TE 用于 TimeEvent,我只想插入它们 vector ,并从 vector 中获取它们,这是我当前的代码:

Event::SampleEvent SE = Event::SampleEvent(1000, 0, 0, 10, 10, 10);
Event::TimeEvent TE = Event::TimeEvent(200, 20, 20, 20);
vector<Event> DataEvent;

// insert Event
DataEvent.push_back(SE);
DataEvent.push_back(TE);

// Now I just want to get it back
Event::SampleEvent RSE = DataEvent[0]; // -> Error no suitable user-defined conversion from "Engine::Data::Event" to "Engine::Data::Event::SampleEvent" exists
Event::TimeEvent RTE = DataEvent[0];   // -> Error no suitable user-defined conversion from "Engine::Data::Event" to "Engine::Data::Event::TimeEvent" exists

// And I don't know how to detecting the inheritance Class
// something like if (RSE == Event::SampleEvent) or if (RTE == Event::TimeEvent) @_@

最佳答案

我相信你需要施放它才能取回它。因为虽然您可以将 SampleEvent 和 TimeEvent 隐式转换为 Event,但您不能隐式地以相反的方式进行。

您将需要使用 Event 的引用或指向 Event 的指针来使其与转换一起正常工作。

使用引用

*removed* you cannot make a vector reference.

使用指针

Event::SampleEvent SE = Event::SampleEvent(1000, 0, 0, 10, 10, 10);
Event::TimeEvent TE = Event::TimeEvent(200, 20, 20, 20);

std::vector<Event*> DataEvent;
// insert Event
DataEvent.push_back(&SE);
DataEvent.push_back(&TE);
// get the events back, note this can throw an exception if you cast incorrectly.
Event::SampleEvent* RSE = (Event::SampleEvent*)DataEvent[0]; 
Event::TimeEvent* RTE = (Event::TimeEvent*)DataEvent[1]; 
/// This also Works using static_cast
//Event::SampleEvent* RSE = static_cast<Event::SampleEvent*>(DataEvent[0]); 
//Event::TimeEvent* RTE = static_cast<Event::TimeEvent*>(DataEvent[1]);  
std::cout << RSE->ID << std::endl;
std::cout << RTE->Value << std::endl;

输出为:1000 200

有关类型转换的更多信息,请参阅此 stackoverflow回答。

关于c++ - 检测继承类并将其转换为基类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15707890/

相关文章:

c++ - 在参数列表中就地声明不完整的类型模板参数

c++ - Base64 编码的字符串太大,尾随字符在 C++ 中被截断

java - dll文件的JNI问题

c++ - C++ 中是否有等效的 str_replace?

c++ - 初始化 vector vector

c++ - QT - 选择图像部分

c++ - 如何创建一个 void 数组,其大小将由用户从命令行输入指定?

c++ - 在 C++ 中存储大量短暂的游戏对象

c++ - 在 C++20 中从有符号整数到无符号整数,反之亦然

c++ - 警告: 'LPEVENT CItem::m_pkExpireEvent' and warning:format '%d' expects type 'int' , but argument 3 has type 'double'