C ++使用指向超类的指针查找子类的类型

C++ finding type of subclass with a pointer to superclass

在我的 C++ 代码中,我有一个指向 Light 对象的指针向量。 P_Light 是 Light 的子类,并且有一个字段位置。对于每个指向实际上是 P_Light 的 Light 的指针,我需要对位置字段做一些事情。我做了一些搜索,似乎我可以实现一个虚拟方法,但我不需要 Light 中的方法,因为其他类型的 Light 没有位置。我也考虑过选角,但我不确定如何让它发挥作用。

std::vector<Vector> light_dirs;
for(int i=0; i<lights.size; i++){
    Light *l = lights[i];   
    //cast here?
}

编辑:: 在另一个 post 中看到,也许使用 qobject_cast 是个好主意。这样更好看吗?

std::vector<Vector> light_dirs;
for(int i=0; i<lights.size; i++){
    Light *l = lights[i];   
    P_Light* pl = qobject_cast<P_Light*>(l);
    if(pl != nullptr) //dostuff;
}

这应该有效。

P_Light* p_light = dynamic_cast<P_Light*>(l);

https://en.wikipedia.org/wiki/Run-time_type_information

上检查 RTTI 和动态投射

正如 Seo 指出的那样,动态转换可能是最直接的方式来快速适应您描述的用例,而无需重构 class 结构。

作为替代方案,您可以考虑使用访问者模式的实现,它提供编译时安全和验证。

class P_Light;
class NP_Light;

/* abstract class providing dispatching based on 
 * whether Light objects interact with the position field */
class Light_Visiter{
  virtual ~Light_Visiter(){}
  virtual void visit(NP_light&) = 0;
  virtual void visit(P_Light&) = 0;
};

class Light{
  virtual ~Light(){}
  virtual void visit(Light_Visitor&) = 0;
  // other methods
};

class P_Light : public Light{
  void visit(Light_Visitor& lv){lv.visit(*self);}
  // other methods
};

/* Light objects which do not interact with the position
 * field inherit from NP_Light */ 
class NP_Light : public Light{
  void visit(Light_Visitor& lv){lv.visit(*self);}
  // other methods
};

这为您提供了与动态转换相同的优势——您的 classes 仍然独立于对其进行操作的算法——但调度逻辑是集中的而不是分散在整个源中。

有关访问者模式的更多信息,请参阅这篇文章article