Topic: confusion about stl list
Author: glgrobe@iwl.net (G.L. Grobe)
Date: 1998/10/12 Raw View
The intention here is to have a list that can hold any type of object. So I
have a pointer to a list of void pointers ( list<void *> *myList ). Below will
show what I'm trying to do but not sure on how to write this.
The push_back function in the 4th line of the first example is incorrect but
doesnt give any compile errors. I'm not sure how to cast this correctly as I
think this might be the problem.
--------
list<void *> *myList; // a list that is intended to hold anytype of objects
myObj *obj = new myObj(parm1, parm2);
obj->set_value(val);
// up till this point is fine, then the push_back routine dumps core here.
myList->push_back(obj);
--------
Another way I thought possible is to somehow assign a new object to an iterator
(which would be obj here), put it on the list, then make my set_value call. Not
sure how to do this either.
---------
list<void *> *::iterator obj = new myObj(parm1, parm2); // of course this
doesn't work
// or
myList->push_back(new myObj(parm1, parm2)); // this line does work
// but then how do I do the following
(*obj)->set_value(val);
---------
Any help would be much appreciated.
TIA,
glgrobe@iwl.net
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1998/10/13 Raw View
G.L. Grobe wrote:
>
> The intention here is to have a list that can hold any type of object. So I
> have a pointer to a list of void pointers ( list<void *> *myList ). Below will
> show what I'm trying to do but not sure on how to write this.
>
> The push_back function in the 4th line of the first example is incorrect but
> doesnt give any compile errors. I'm not sure how to cast this correctly as I
> think this might be the problem.
The push_back call is indeed correct, assuming a valid myList (the
conversion myObj* -> void* is implicit).
> --------
> list<void *> *myList; // a list that is intended to hold anytype of objects
Here you're wrong. You only have a pointer to a (not yet created)
list<void*>.
Add:
myList = new list<void*>();
> myObj *obj = new myObj(parm1, parm2);
> obj->set_value(val);
> // up till this point is fine, then the push_back routine dumps core here.
> myList->push_back(obj);
Here you are dereferencing an uninitialized pointer: myList.
Therefore it dumps core (it may instead cause more subtle bugs,
depending on the random value in myList).
> Another way I thought possible is to somehow assign a new object to an iterator
> (which would be obj here), put it on the list, then make my set_value call. Not
> sure how to do this either.
> ---------
> list<void *> *::iterator obj = new myObj(parm1, parm2); // of course this
> doesn't work
This has quite a lot of errors indeed.
First, it tries to access the member "iterator" of the
pointer type list<void*>*, but pointers have no members.
It's the class which has members. Write:
list<void*>::iterator obj;
Next, you try to initialize the iterator with the pointer to your
new object. This is wrong again - iterators are similar to
pointers, but they are not just pointers. (Actually, vector
iterators are often implemented as pointers; so if you had used
vector instead of list, the compiler might not even have
warned you about this error).
> // or
> myList->push_back(new myObj(parm1, parm2)); // this line does work
With the definition of myList above? Then it's just bad luck.
(Yes: Bad luck, because you are not told that you made a mistake).
However, if this time, you had actually created a list, this is
supposed to work.
> // but then how do I do the following
> (*obj)->set_value(val);
(myObj&)(*myList->end())->set_value(val);
Of course, this depends on the fact that this is indeed a myObj.
This is the reason why list<void*> is generally considered bad:
It completely bypasses typesafety.
You should consider if you _really_ need to put _arbitrary_
objects in your list. A better solution would be to provide
a common base class, and to put pointers to that into your list.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]