如何将基类指针数组强制转换为派生类
How to cast array of base class pointer to derived class
我们如何将基类指针数组强制转换为派生类指针数组。下面的代码显示了我正在尝试执行的操作的简单示例:
class CBaseClass
{
public :
int CommonValue;
};
class CFirstItem : public CBaseClass
{
public :
int firtValue;
};
class CSecondItem : public CBaseClass
{
public :
int secondValue;
};
class CThirdItem : public CBaseClass
{
public :
int thirdValue;
};
class Manager
{
public :
Manager()
{
for (unsigned int index = 0; index < 5; ++index)
{
m_firstItem[index] = new CFirstItem();
}
for (unsigned int index = 0; index < 10; ++index)
{
m_secondItem[index] = new CSecondItem();
}
for (unsigned int index = 0; index < 12; ++index)
{
m_thirdItem[index] = new CThirdItem();
}
}
private :
CBaseClass* m_firstItem[5];
CBaseClass* m_secondItem[10];
CBaseClass* m_thirdItem[12];
};
我在这里的主要问题是我如何从CBaseClass
投射到CFirstClass
或CSecondClass
,我尝试过
CFirstClass* wFirstClass = static_cast<wFirstClass*>(m_firstItem);
这似乎行不通,有什么办法可以制作这个演员表吗?
CFirstClass* wFirstClass = static_cast<wFirstClass*>(m_firstItem);
是错误的,因为w_firstItem
在上面的表达式中衰减到CBaseClass**
。
如果你能设计你的程序,这样你就不需要投射了,你会过得更好。如果必须强制转换,则需要使用dynamic_cast
.
CFirstClass* wFirstClass = dynamic_cast<CFirstClass*>(m_firstItem[0]);
if ( wFirstClass )
{
// Use the pointer.
}
else
{
// Deal with other cases
}
如果需要在m_firstItem
中投射每个指针,则需要使用循环。
for ( auto item : m_firstItem )
{
CFirstClass* wFirstClass = dynamic_cast<CFirstClass*>(item);
if ( wFirstClass )
{
// Use the pointer.
}
else
{
// Deal with other cases
}
}
更新
@RemyLebeau提出了一个很好的观点。仅当m_firstItem
包含指向不同子类型的CBaseClass
的指针时,才应使用dynamic_cast
。在您发布的代码中,由于它只包含指向CFirstClass
的指针,因此可以使用static_cast
而不是dynamic_cast
:
CFirstClass* wFirstClass = static_cast<CFirstClass*>(m_firstItem[0]);
循环版本同样可以使用static_cast<CFirstClass*>(item)
。
不能简单地将基指针数组类型转换为派生指针数组。 指针不会指向正确的地址。 如果你需要一个派生指针数组,你必须创建一个单独的数组并正确地对源项进行类型转换,例如:
CFirstClass* wFirstClass[5];
for (int i = 0; i < 5; ++i)
wFirstClass[i] = static_cast<CFirstClass*>(m_firstItem[i]);
与其他数组相同:
CSecondClass* wSecondClass[10];
for (int i = 0; i < 10; ++i)
wSecondClass[i] = static_cast<CSecondClass*>(m_secondItem[i]);
CThirdClass* wThirdClass[12];
for (int i = 0; i < 12; ++i)
wThirdClass[i] = static_cast<CThirdClass*>(m_thirdItem[i]);
相关文章:
- 存储模板类型以强制转换回派生<T>
- 在 C++ 中将对象转换为派生类型
- 从基类实例调用派生类方法而不进行强制转换
- 从基指针到派生的强制转换问题
- C++将派生类转换为基类时'object slicing'期间发生的情况
- 派生类(构造函数具有参数)和基类(构造函数缺少参数)之间没有可行的转换
- C ++基础私有方法在将自身转换为派生类后可以访问吗?
- 调用不在基类中的派生类函数而不进行动态强制转换,以最大程度地提高性能
- 在将派生类指针类型转换为派生类指针后,从基类指针调用派生类函数
- 我们可以在不知道其真实类型的情况下将基类指针转换为派生类指针吗?
- 为什么从基转换为派生提供此功能?
- 将基本实例指针强制转换为派生实例指针是否合法?(实例不是派生实例)
- 从从该抽象类派生的 Python 对象强制转换C++抽象类C++
- 基类到派生类的强制转换向量
- 有没有办法复制派生类指针的向量而不将其强制转换为基类?
- 为什么此代码不将基类强制转换为 c++ 中的派生类?
- 如何在运行时将指向派生类的 void* 正确转换为指向基类的 void*
- C++转换派生类
- CRTP / 宏 / 避免强制转换派生类的指针
- 如何正确转换派生类