design problem: getting runtime type information

A

alessio211734

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.
 
A

Andrea Crotti

Something like this compiles to me
#include <iostream>
#include <list>
#include <string>
using namespace std;
enum CLASS_ID { LIGHT, MESH, MARKER};

class NodeScene
{
private:
list<NodeScene> * m_child;
CLASS_ID id;

protected:
string icon;

public:
virtual void setIcon() {
//setTreeViewIcon(icon);
}
};

class Mesh : public NodeScene
{
public:
Mesh() {
string s("mesh");
icon = s;
}
};

not sure is exactly what you want..
I wanted to have the icon initialization in the initialization of the
constructor but the on the fly string construction doesn't work
apparently..
 
F

Fred Zwarts

Andrea Crotti said:
Something like this compiles to me
#include <iostream>
#include <list>
#include <string>
using namespace std;
enum CLASS_ID { LIGHT, MESH, MARKER};

class NodeScene
{
private:
list<NodeScene> * m_child;
CLASS_ID id;

protected:
string icon;

public:
virtual void setIcon() {
//setTreeViewIcon(icon);
}
};

class Mesh : public NodeScene
{
public:
Mesh() {
string s("mesh");
icon = s;
}
};

not sure is exactly what you want..
I wanted to have the icon initialization in the initialization of the
constructor but the on the fly string construction doesn't work
apparently..

Apparently? I don't see compiler errors nor run-time errors.
See the FAQ about posting code that does not work as expected.
Give at least sufficient code to reproduce the problem.
Tell what you expect and how that differs from the result you get.
 
A

Andrea Crotti

Apparently? I don't see compiler errors nor run-time errors.
See the FAQ about posting code that does not work as expected.
Give at least sufficient code to reproduce the problem.
Tell what you expect and how that differs from the result you get.

No no what I pasted compiles and run, what I wanted to do instead
doesn't, that's what I meant...

I meant do something like
Mesh() : icon("mesh")
 
F

Fred Zwarts

Andrea Crotti said:
No no what I pasted compiles and run, what I wanted to do instead
doesn't, that's what I meant...

I meant do something like
Mesh() : icon("mesh")

icon is not a member of Mesh, so it cannot be initialize by Mesh.
Probably the best method is to add a constructor for NodeScene
with a string parameter, so that you can initialize icon in the constructor
of NodeScene and use this constructor in the constructor of Mesh

NodeScene (const string & NewIcon) : icon (NewIcon) {
....
}

Mesh () : NodeScene ("mesh") {
....
}
 
R

Ruslan Mullakhmetov

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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top