如何从基指针推断类的子类型
How to deduce subtype of a class from a base pointer
我有一个基类(Notification)和两个子类(WebNotification和DesktopNotification)。我从外部源接收WebNotifications和DesktopNotifications,然后我将它们作为通知指针传递给GUI实体,该实体应该根据通知的类型做不同的事情。如何根据通知*推断子类型?
我知道在Notification类中添加一个带有类型的enum可以工作,但这似乎不是一个优雅的解决方案。
我有其他选择吗?
有多种选择。
首先,专门化行为应该是类的一部分,而不是客户端的一部分。这意味着你应该避免动态检查类的类型,除非你真的被迫这样做。
这通常通过多态性实现:
class Notification {
public:
virtual void render(UI* ui) = 0;
};
class WebNotification : public Notification {
public:
void render(UI* ui) override {
....
}
};
在某些情况下,这是不够的,所以你可以选择标记你的类,例如
class Notification {
public:
enum class Type {
WEB,
DESKTOP
};
private:
Type type;
protected:
Notification(Type type) : type(type) { }
public:
Type getType() const { return type; }
};
class WebNotification : public Notification {
public:
WebNotification() : Notification(Notification::Type::WEB) { }
};
...
Notification* notification = ...
if (notification->getType() == Notification::Type::WEB)
...
或者让编译器为您标记它们,但是您必须启用RTTI,有时这是不可取的:
if (dynamic_cast<WebNotification*>(notification) != null) {
...
}
使用动态类型转换,即
Notification* notification;
WebNotification* web_notification = dynamic_cast<WebNotification*>(notification);
if ( web_notification != NULL ) {
// this is a web notification;
}
DesktopNotification* dt_notification = dynamic_cast<DesktopNotification*>(notification);
if ( dt_notification != NULL ) {
// this is a desktop notificaiton
}
相关文章:
- 非类型指针和引用模板参数,以及在编译时如何/为什么解析它们.c++
- 如何访问在 c++ 中在类内声明的结构类型指针变量?
- (C )找到基本类型指针的儿童类型
- 表达式必须具有指向对象的指针类型(指针向量)
- 将子项复制构造到父类型指针中
- C 返回类型指针声明
- 调用虚拟函数而不通过类类型指针创建任何对象
- C++ 如何使用类类型指针制作向量
- 为什么基类型指针不能获取派生类对象的地址值?
- 如何在 C++ 中获取映射类型指针,映射
- 如何检查该类型的类型指针是正确对齐的
- 正在转换为短类型指针
- 指向任意类方法的模板非类型指针
- 如何通过强制转换类型指针将字符数组转换为uint16_t
- 指向shared_ptr的不透明类型 C 指针
- C++:从值类型指针强制转换为包含迭代器
- C/C++:访问给定类型指针的位置与访问另一类型指针的相同位置不同
- 比较类型指针
- 数据类型指针使用*(Datatype *)
- 如何打印c++中char类型指针的所有值