Sign in to follow this  
ehmdjii

C++ inheritance

Recommended Posts

ehmdjii    238
hello, i am having a problem with inheritance in C++. i have a class "node" that defines a virtual function render(). now other classes that extend from node overwrite this render-function. after that i put all instance of any of those classes in a list and call the render-function. now i expect that the corresponding render-function in the subclass is being called, but unfortunately the render-function from the baseclass "node" is always called. let me know if i should provide some code to illustrate the problem. thanks!

Share this post


Link to post
Share on other sites
SiCrane    11839
Does your list store nodes by value? If so, then you probably have a slicing issue; only the base part of the objects are being copied into your list. Try using a list of (smart) pointers.

Share this post


Link to post
Share on other sites
ehmdjii    238
thank you here is the code


node * node1 = new node();
node * node2 = new cat();
node * tpu = new powerup();

tlist->push_back(node1);
tlist->push_back(node2);
tlist->push_back(tpu);

list<node>::iterator theIterator;
for( theIterator = tlist.begin(); theIterator != tlist.end(); theIterator++ ) {
theIterator->render();
}


Share this post


Link to post
Share on other sites
Split Thumbs    112
Ah. So SiCrane was right, only it is a little trickier. Your iterator is splicing the information into a node, try:


list<node*>::iterator theIterator;
for( theIterator = tlist.begin(); theIterator != tlist.end(); theIterator++ ) {
(*theIterator)->render();
}



I think that will work.

Share this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by ehmdjii
thank you here is the code

*** Source Snippet Removed ***


No, it isn't. First of all, you can't be treating 'tlist' as a pointer in some places (the push_back calls) and as a list directly in others (when you create the iterators), and second, you can't put node*'s into a list of node's. Try again.

Share this post


Link to post
Share on other sites
ehmdjii    238
ok, i was doing some shortcuts, when typing in the code.
here is the actual code

tnode * node1 = new tnode();
tnode * node2 = new tnode();
tnode * tpu = new tpowerup();

tscenegraph * sg = new tscenegraph();
sg->add(node1);
sg->add(node2);
sg->add(tpu);

sg->render();



and the scenegraph class

tscenegraph::tscenegraph(void)
{
nodes = new vector<tnode>();
}

tscenegraph::~tscenegraph(void)
{
}

void tscenegraph::add(tnode * node)
{
nodes->push_back( *node );
}

void tscenegraph::render(void)
{
vector<tnode>::iterator theIterator;
for( theIterator = nodes->begin(); theIterator != nodes->end(); theIterator++ ) {
(*theIterator).render();
}
}



thanks!

Share this post


Link to post
Share on other sites
rip-off    10976
Your code is a pointer to vector, not a vector of pointers [grin]. Lesson: when you have the code in front of you, it is a crime not to post it.



tnode * node1 = new tnode();
tnode * node2 = new tnode();
tnode * tpu = new tpowerup();

tscenegraph * sg = new tscenegraph();
sg->add(node1);
sg->add(node2);
sg->add(tpu);

sg->render();





class tscenegraph
{
public:

// ...

private:
vector<tnode *> nodes;
};

tscenegraph::tscenegraph()
{
}

tscenegraph::~tscenegraph(void)
{
// clean up the nodes
}

void tscenegraph::add(tnode * node)
{
nodes.push_back( node );
}

void tscenegraph::render()
{
vector<tnode*>::iterator theIterator;
for( theIterator = nodes.begin(); theIterator != nodes.end(); theIterator++ ) {
theIterator->render();
}
}


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this