获取参数的子类

Get child class of argument

本文关键字:子类 参数 获取      更新时间:2023-10-16

我有一个获取父类作为参数的方法。 我需要在编译时知道该参数的子类是什么。

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 的参数。