1

我有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 个变量,SE对于SampleEvent 和 TE 的 TimeEvent TESE我只想将它们插入向量,并从向量中获取它们,这是我当前的代码:

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) @_@
4

3 回答 3

1

我相信您需要将其投射以将其取回。因为虽然您可以将 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答案。

于 2013-03-29T17:14:14.030 回答
0

你有一个向量

vector<Event> DataEvent;

所以你应该这样使用它:

Event E = DataEvent[0];

如果您键入Event::SampleEvent RSE = DataEvent[0];,那么您没有使用指向具有基类指针的子类的能力,而只是简单地转换对象。如果您希望此强制转换成功,您必须提供转换运算符,或者考虑使用指针向量:vector<Event* > DataEvent;然后如果您想要获取特定事件,您可以使用dynamic_cast<>它允许您动态获取子类的对象,前提是它实际上是这个子类对象:记住你可以在公共基类下的向量中有许多不同的子类,你还需要一些虚拟方法,否则类型不被视为多态,你不能使用dynamic_cast<>. 添加就足够了

virtual void f(){}

到事件类

于 2013-03-29T17:16:58.203 回答
0

不可能将子类对象 itsef转换为基类对象,而是可以轻松地将子类对象的引用(如指针)转换为基类的引用。

于 2013-03-29T17:20:16.837 回答