C++ inheritance

Started by
9 comments, last by ehmdjii 16 years, 4 months ago
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!
Advertisement
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.
Hm. Thats not right. Something must be going wrong somewhere. A little code wouldn't hurt ;-)
Oh. I had assumed they were not by value, SiCrane could be right.
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();}
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.
thanks, but that still does not work :/
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.
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!
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();	}  }

This topic is closed to new replies.

Advertisement