我有一些这样的类:
interface class IA
{
};
interface class IB
{
};
public ref class C : public IA, public IB
{
};
public ref class D
{
void DoSomething(IA^ aaa)
{
}
void Run()
{
C^ bob = gcnew C();
DoSomething(dynamic_cast<IA^>(bob)); // #1
DoSomething(bob); // #2
}
};目前,在调用这样的函数时,我总是尝试使用动态类型转换(上面的#1 )。
然而,它确实让代码变得相当丑陋,所以我想知道它是否真的有必要。
你以这种方式使用dynamic_cast吗?如果是这样,主要原因是什么?
发布于 2009-10-23 22:14:20
在标准C++中,您使用dynamic_cast向下遍历层次结构,而不是向上遍历。在本例中,您将使用它尝试将IA或IB转换为C:
IA^ temp = /* get a C in some way. */;
C^ tempC = dynamic_cast<C^>(temp);https://stackoverflow.com/questions/1613706
复制相似问题