من در حال یاد گیری tyepcasting از کلاس پایه به کلاس فرزند هستم این مثالی که در ادامه قرار دام به شما نشان میده که من چه کاری انجام دادم در ضمن می دونم که راه حلم درست نیست من به دنبال یک ساختار پویا تر و بهتر هستم.
enum EComponentType
{
Base = 0,
Material = 1
};
class FComponent
{
public:
FComponent() { m_type = EComponentType::Base; }
EComponentType type() { return m_type; }
protected:
EComponentType m_type;
};
class FMaterial : public FComponent
{
public:
FMaterial() { m_type = EComponentType::Material; }
void setValue(int value) { m_value = value; }
int value() { return m_value; }
private:
int m_value;
};
int main(int argc, char *argv[])
{
FMaterial material;
material.setValue(22);
QVector<FComponent> Components;
Components << material;
for (int i = 0; i < Components.size(); i++)
{
switch (Components[i].type())
{
case EComponentType::Material:
FMaterial material = (FMaterial)(Components[i]); // --> invalid of course
int value = material.value();
break;
}
}
return 0;
}