Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

sdoherty55

STL List of base Class Object

This topic is 6010 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is is possible to create a single STL List of objects through a common base class? When I try to implement this, all I get is an error becuase the object being added to the list is the inherited class? Clearly, I can cast the inherited class to the base class, but won''t I lose the inherited functionality? Thanks

Share this post


Link to post
Share on other sites
Advertisement
It works fine for me.

  
class A
{
public:
virtual void act(void)
{
cout << "A" << endl;
}
};

class B : public A
{
public:
virtual void act(void)
{
cout << "B" << endl;
}
};


int main(int argc, char* argv[])
{
list<A*> myList;
myList.push_back(new A);
myList.push_back(new B);
for(list<A*>::iterator it = myList.begin(); it != myList.end(); ++it)
{
(*it)->act();
delete *it;
*it = NULL;
}
myList.pop_back();
myList.pop_back();
return 0;
}

Share this post


Link to post
Share on other sites
You probably created a list of objects rather than a list of pointers to objects. Happens pretty frequently (see DrPizza's post for details; note he declares std::list<A *>myList;).

[Edit:] Highlighting.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!


Edited by - Oluseyi on February 3, 2002 2:50:12 PM

Share this post


Link to post
Share on other sites
If you want your old code to work without doing thinks like changing this...

A.stuff()

...into this...

A->stuff()

...then you can create a list of references instead. Simply use ampersand instead of an asterisk:

list<A&>


Edited by - TerranFury on February 3, 2002 3:08:39 PM

Share this post


Link to post
Share on other sites
quote:
Original post by TerranFury
Simply use ampersand instead of an asterisk

You can always dereference the pointer and still use dot notation:

list<A *>::iterator i = myList.being(),
end = myList.end();
while(i != end)
{
(*(*i)).method(); // same as (*i)->method();
++i;
}

Why changing from dot to arrow notation would be such a big deal...

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
quote:
Original post by TerranFury
If you want your old code to work without doing thinks like changing this...

A.stuff()

...into this...

A->stuff()

...then you can create a list of references instead. Simply use ampersand instead of an asterisk:

list



No you can''t.

It forms all sorts of illegal pointer types if you try such a thing, and even if it were to work, it would lead to all sorts of unpleasant complications due to the way references work (in particular, issues arise whenever you have a reference to a temporary).

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!