获取参数的子类
Get child class of argument
我有一个获取父类作为参数的方法。 我需要在编译时知道该参数的子类是什么。
class A{
...
};
class B: public A{
...
};
class C: public A{
...
};
void foo(A a){
//is a specialized in B or C?
}
C c;
foo(c); //here it is C
你的期望背后的概念是一种编译时多态性。
您可以通过函数重载来实现它。
编写两个不同的函数没有错(正如 DimChtz 在评论中所说(......一个带有类型 B 的参数,另一个带有类型 C 的参数。
相关文章:
- 使用模板参数重载C++方法:如何使其适用于模板的子类?
- 父类的私有函数会导致对具有相同名称和相似参数的子类中的公共函数的不明确调用
- 如何将子类作为函数的参数传递给期望基类,然后将该对象传递到指向这些抽象类对象的指针向量中?
- 在子类函数覆盖中省略具有默认值的参数
- 将方法参数类型更改为子类中的派生类
- 获取参数的子类
- 无法将参数 3 从 'const QGraphicsItem *' 转换为 'const QObject *',这是指针,对象是 QGraphicsObject 的子类
- 子类化 std::thread:构造器中可变参数模板函数的问题
- 子类作为模板模板基类的模板参数,而该类别又是子类函数参数
- 在子类中定义可变参数函数专用化
- 是否有必要调用父构造函数而不使用子类构造函数的参数?
- 如何以相同的方法在子类中超载的相同方法来实现工作变量参数方法
- 类型的C 参数与父母和子类的类型参数不相容
- 将子类作为构造函数中的参数
- 如何在子类方法中更改参数
- c++将子类作为参数传递
- C++ 不同的子类需要不同的参数
- 将基类 2 参数构造函数调用到子类 1 参数构造函数中
- C++-使指向超类的指针与函数中的子类参数匹配
- CRTP+可变模板+提取CRTP子类参数