- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有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
和 TE
,SE
用于 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/
经过几个小时的(重新)搜索,我无法想出普通抽象类和使用模板模式之间的可解释区别。 我唯一看到的是: 使用抽象类时,您需要实现所有方法。但是在使用模板方法时,您只需要实现这两个抽象方法。 有人可以向我解
我正在尝试实现一种算法,该算法可找到以下形状给出的外多边形的每个单独边的对应区域。也就是说,1,2 边的相应区域是 [1,6,7,8,2],2,3 边的区域是 [2,8,3] 等等,CCW 或 CW
我正在尝试在派生 self 的 BaseController 类的任何 Controller 上自动设置一个属性。这是我的 Application_Start 方法中的代码。 UnitOfWork 属
我正在使用 mgcv 包通过以下方式将一些多项式样条拟合到一些数据: x.gam smooth$knots [1] -0.081161 -0.054107 -0.027053 0.000001
考虑以下代码: void foo(){ ..... } int main() { int arr[3][3] ; char string[10]; foo();
本书The c++ programming language有这个代码: class BB_ival_slider : public Ival_slider, protected BBslider {
是否有一个 package.json 属性可用于指定模块解析应启动的根文件夹? 例如,假设我们在 node_modules/mypackage/src/file1 中有一个安装。我们要导入的所有文件都
我正在尝试使用聚合函数来实现与 SQL 查询相同的结果: 查询语句: sqldf(" SELECT PhotoID, UserID,
我正在比较使用 LOESS 回归的两条线。我想清楚地显示两条线的置信区间,我遇到了一些困难。 我尝试过使用各种线型和颜色,但在我看来,结果仍然是忙碌和凌乱。我认为置信区间之间的阴影可能会使事情变得更清
给定这段代码 public override void Serialize(BaseContentObject obj) { string file = ObjectDataStoreFold
我正在构建某种工厂方法,它按以下方式将 DerivedClass 作为 BaseClass 返回: BaseClass Factory() { return DerivedClass(); }
当重写 class delegation 实现的接口(interface)方法时,是否可以调用通常从重写函数中委托(delegate)给的类?类似于使用继承时调用 super 的方式。 来自docum
我有一个基类 fragment (如下所示)。我在其他 3 个 fragment 类中扩展了此类,每个类都共享需要在这 3 个 fragment 中访问的相同 EditText。因此,我在基类中设置了
如何在不加载额外库的情况下在 R 中计算两个排列之间的 Kendall tau 距离(又名冒泡排序距离)? 最佳答案 这是一个 O(n.log(n)) 的实现,在阅读后拼凑而成,但我怀疑可能有更好的
情况 我创建了一个具有国际化 (i18n) 的 Angular 应用程序。我想在子域中托管不同的版本,例如: zh.myexample.com es.myexample.com 问题 当我使用命令 n
std::is_base_of 之间的唯一区别和 std::is_convertible是前者在 Base 时也成立是 私有(private)或 protected Derived 的基类.但是,您何
我创建了一个名为 baseviewcontroller 的父类(super class) uiviewcontroller 类,用于包含大多数应用屏幕所需的基本 UI。它包括一个自定义导航栏和一个“自
我是一名优秀的程序员,十分优秀!