C++ 中模板化类型的类层次结构
Class hierarchy in templated types in C++
我正在做一个简单的项目,其中有发布者从一个数据源读取并为订阅者构建事件。
事件将具有数据的唯一 PTR,订阅者将具有事件的共享 PTR。
我希望有一个事件的通用接口,以便订阅者可以订阅任意数据发布者。 但发布者只能发布一种类型的数据。
#include <memory>
#include <iostream>
class Data
{
};
class ChildData : public Data
{
};
template<typename T>
class Event
{
std::unique_ptr<T> _data;
};
void someFunc(Event<Data> event)
{
std::cout << "hello Word" << std::endl;
}
int main()
{
Event<ChildData> event();
someFunc(event);
return 0;
}
但是我收到以下编译器错误
/home/rory/dev/cpp_sandbox/main.cpp: In function ‘int main()’:
/home/rory/dev/cpp_sandbox/main.cpp:27:19: error: could not convert ‘event’ from ‘Event<ChildData> (*)()’ to ‘Event<Data>’
someFunc(event);
^
CMakeFiles/example.dir/build.make:62: recipe for target 'CMakeFiles/example.dir/main.cpp.o' failed
make[2]: *** [CMakeFiles/example.dir/main.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/example.dir/all' failed
make[1]: *** [CMakeFiles/example.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
这种事情在C++可能吗?
有问题的一行是这样的:
Event<ChildData> event();
根据最令人烦恼的解析规则,这被解释为函数声明。如果Event
类具有初始化所有成员的默认构造函数,则可以省略括号。否则使用统一初始化语法:
Event<ChildData> event{};
此外,您可能必须使用std::move
将event
传递给someFunc
,因为Event
由于数据成员std::unique_ptr
而不可复制。此外,您还必须实现从Event<ChildData>
到Event<Data>
的转换,这是someFunc
所必需的。
我希望有一个事件的通用接口,以便订阅者可以订阅任意数据发布者。但发布者只能发布一种类型的数据。
从这个描述来看,Event
模板似乎是多余的。Data
后代的公共接口可以在Data
类本身中描述,因此除非需要将事件与数据分开,否则仅处理数据会更容易。
为了区分不同的事件类型,您可以使用枚举。
enum class EventType
{
A,
B,
C
};
class Data
{
private:
const EventType m_type;
protected:
explicit Data(EventType type) : m_type(type) {}
public:
virtual ~Data() = default;
// Prohibit copying to avoid accidental slicing
Data(Data const&) = delete;
Data& opreator= (Data const&) = delete;
EventType get_type() const { return m_type; }
};
class ChildData :
public Data
{
public:
ChildData() : Data(EventType::A) {}
};
void receive(std::unique_ptr< Data > event)
{
switch (event->get_type())
{
case EventType::A:
{
std::unique_ptr< ChildData > child_event{ static_cast< ChildData* >(event.release()) };
// Process ChildData
}
break;
case EventType::B:
// ...
case EventType::C:
// ...
}
}
void publish(std::unique_ptr< Data > event);
int main()
{
std::unique_ptr< ChildData > event{ new ChildData() };
// Fill ChildData event
publish(std::move(event));
}
相关文章:
- C++ 中模板化类型的类层次结构
- 为什么不同类型层次结构的指针之间的dynamic_cast定义得很好?
- 确定大层次结构中基本指针的实际类型,无需dynamic_cast
- 模板冲突的类型-但类型应该是相同的cfr类层次结构
- Sean Parent:对于继承层次结构中的多态类型,具有可变对象是极端的例外
- 检查类是否在继承层次结构中显式定义了成员类型
- Eclipse Neon.3 更改类型层次结构/调用层次结构中的颜色
- 在模板的层次结构中嵌入类型声明
- 在c++模板生成的类层次结构中,按参数类型匹配类
- 静态调度以使用聚合类型的层次结构更正成员方法
- c++设计:避免使用现有的类层次结构对类型进行迭代
- 在 C++ 的异常层次结构中引入其他基类型
- 有了父子类层次结构,如何在单个 "Parent" 类型变量中保存各种实际子级?
- 使用类型继承的数字组的层次结构
- 继承层次结构中的特定类作为boost::signal2回调中的类型
- 层次结构中的对象类型检测
- 如何在层次结构头文件中声明嵌套类型
- 如何在二维类型层次结构中提供类型安全
- 类标记的层次结构,并在解析器中检查其类型
- 如何编写一个类层次结构来方便地在浮点类型之间切换