Skip to main content
gamedev.net
🔒 Locked

inheritance ?

Started by EvilCrap Dec 6, 2001 at 4:48 PM 0 replies 300+ views
Original Post
EvilCrap
EvilCrap
say, how can i create a linked list of different classes? i think it has something to do with inheritance, but i dont recall. like, where each class has a common one, "a" class a; class b:a; class c:a; //this is psuedo code since i dont actually know how a* x = new (a)b; a* y = new (a)c; x->next = y; do ya get the drift of what i want to do?
invective
invective
You make the list a bunch of pointers to the base class.

  
#include <iostream>
#include <list>

class parent {
public:
virtual void print ()
{
std::cout << "i am a parent";
}
};

class child : public parent {
public:
virtual void print ()
{
std::cout << "i am a child";
}
};

void main (void)
{
parent *parentPtr;
child childObject;
parentPtr=&childObject

std::list <parent *> objectList;
objectList.push_back (parentPtr);

//Fetch first object from list

parent * someObject = *( objectList.begin() );

//this will call the child print function, because the function is

//virtual (dynamically bound)

someObject->print();
}


Brush up on your polymorphism / virtual functions here is you do not remember:
http://www.parashift.com/c++-faq-lite/virtual-functions.html

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.