题
使用 sizeof
运算符,我可以确定任何类型的大小–但是如何在运行时动态确定多态类的大小?
例如,我有一个指向 Animal
的指针,我希望得到它指向的实际对象的大小,如果它是 Cat <代码>或
Dog
。有没有一种简单的方法可以做到这一点,没有创建虚拟方法 Animal :: size
并重载它以返回每种特定类型的 sizeof
?
解决方案
如果您知道可能类型的集合,则可以使用RTTI通过 dynamic_cast
找出动态类型。如果不这样做,唯一的方法是通过虚函数。
其他提示
或者您可以使用typeid,它可能比dynamic_cast快(也可以使用dynamic_cast转换为层次结构中的中间类型)。
看起来很糟糕:
#include <iostream>
#include <typeinfo>
class Creature
{
char x[4];
public:
virtual ~Creature() {}
};
class Animal: public Creature { char x[8];};
class Bird: public Creature { char x[16]; };
class Dog: public Animal { char x[32]; };
class Cat: public Animal { char x[64]; };
class Parrot: public Bird { char x[128]; };
unsigned creature_size(const Creature& cr)
{
if (typeid(cr) == typeid(Animal)) {
return sizeof (Animal);
}
else if (typeid(cr) == typeid(Dog)) {
return sizeof(Dog);
}
else if (typeid(cr) == typeid(Cat)) {
return sizeof(Cat);
}
else if (typeid(cr) == typeid(Bird)) {
return sizeof(Bird);
}
else if (typeid(cr) == typeid(Parrot)) {
return sizeof(Parrot);
}
else if (typeid(cr) == typeid(Creature)){
return sizeof(Creature);
}
assert(false && "creature_size not implemented for this type");
return 0;
}
int main()
{
std::cout << creature_size(Creature()) << '\n'
<< creature_size(Animal()) << '\n'
<< creature_size(Bird()) << '\n'
<< creature_size(Dog()) << '\n'
<< creature_size(Cat()) << '\n'
<< creature_size(Parrot()) << '\n' ;
}
对于每种新类型,您都需要向creature_size函数添加代码。使用虚拟大小功能,您还需要在每个类中实现此功能。但是,这个功能将会非常简单(完全可以复制n-pasteable,这表明语言可能存在限制,代码设计也存在问题):
virtual unsigned size() const { return sizeof(*this); }
你可以在基类中将它抽象化,这意味着如果你忘记覆盖这个方法,它将是一个编译器错误。
编辑:这自然是假设给定任何生物你想知道它的大小。如果您有充分的理由相信您正在处理Dog - 或Dog的子类(并且您不关心它是否是子类),那么您自然可以将dynamic_cast用于 ad hoc test。
如果您能够更改源类的设计,则可以完全用静态多态替换动态多态(使用虚函数)并使用 CRTP成语:
template <class TDerived>
class Base
{
public:
int getSize()
{ return sizeof(TDerived); }
void print()
{
std::cout
<< static_cast<TDerived*>(this)->getSize()
<< std::endl;
}
int some_data;
};
class Derived : public Base<Derived>
{
public:
int some_other_data1;
int some_other_data2;
};
class AnotherDerived : public Base<AnotherDerived>
{
public:
int getSize()
{ return some_unusual_calculations(); }
// Note that the static_cast above is required for this override to work,
// because we are not using virtual functions
};
int main()
{
Derived d;
d.print();
AnotherDerived ad;
ad.print();
return 0;
}
当程序所需的多态行为可以在编译时确定时(例如 sizeof
情况),你可以这样做,因为CRTP没有灵活性动态多态在运行时解析所需的对象。
通过消除虚函数调用开销,静态多态性还具有更高性能的优势。
如果您不想模板化Base类,或者您需要在同一位置(如数组或向量)中保存Base类的不同派生实例,则可以在中间类上使用CRTP并移动多态行为到该类(类似于维基百科中的多态复制构造示例):
class Base
{
public:
virtual int getSize() = 0;
void print()
{
std::cout << getSize() << std:endl;
}
int some_data;
};
template <class TDerived>
class BaseCRTP: public Base
{
public:
virtual int getSize()
{ return sizeof(TDerived); }
};
class Derived : public BaseCRTP<Derived>
{
// As before ...
};
class AnotherDerived : public BaseCRTP<AnotherDerived>
{
// As before ...
// Note that although no static_cast is used in print(),
// the getSize() override still works due to virtual function.
};
Base* obj_list1[100];
obj_list1[0] = new Derived();
obj_list1[2] = new AnotherDerived();
std::vector<Base*> obj_list2;
obj_list2.push_back(new Derived());
obj_list2.push_back(new AnotherDerived());
- 点击
更新:我现在在stackoverflow上找到了类似但更详细的答案,这解释了如果我们进一步从上面的派生类派生(例如 class FurtherDerived:public Derived {...}
), sizeof
将无法正确报告。他提供了代码的更复杂的变体来克服这个问题。
我无法相信有人发明了type_id()而不是实现适当的特征....