list<ClassName*>

I have a question about this code -a different one from the earlier-
Why this:
1
2
3
4
5
6
7
8
9
 
typedef std::list<DrawableObject*> DrawableList;
 
 DrawableList drawableList;

 for(DrawableList::const_iterator iter = drawableList.begin(), 
    endIter = drawableList.end();
    iter != endIter;
    ++iter)



and not this:

1
2
3
4
5
6
7
typedef std::list<DrawableObject*> drawableList;

 for(DrawableList::const_iterator iter = drawableList.begin(), 
    endIter = drawableList.end();
    iter != endIter;
    ++iter)


In the first we have list<Class*> lists;
lists list1;
list1.begin()

In the second there is list<Class*> lists;
lists.begin()

This is the whole code (I found this in a website and note that some declarations are missing)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  class DrawableObject 
 {
  public:
    virtual void Draw(GraphicalDrawingBoard&) const = 0; //draw to GraphicalDrawingBoard
 };
 
 class Triangle : public DrawableObject
 {
 public:
   void Draw(GraphicalDrawingBoard&) const; //draw a triangle
 };
 
 class Rectangle : public DrawableObject
 {
 public:
   void Draw(GraphicalDrawingBoard&) const; //draw a rectangle
 };
 
 class Circle : public DrawableObject
 {
 public:
   void Draw(GraphicalDrawingBoard&) const; //draw a circle
 };
 
 typedef std::list<DrawableObject*> DrawableList;
 
 DrawableList drawableList;
 GraphicalDrawingBoard drawingBoard; //where GraphicalDrawingBoard is defined? why is this legal? 
 
 drawableList.pushback(new Triangle());
 drawableList.pushback(new Rectangle());
 drawableList.pushback(new Circle());
 
 for(DrawableList::const_iterator iter = drawableList.begin(), 
    endIter = drawableList.end();
    iter != endIter;
    ++iter)
 { 
   DrawableObject *object = *iter;
   object->Draw(drawingBoard);
 }


Thank you in advance
In the second version, drawableList is a type, not a variable, so the rest of the code doesn't compile.

Note that in both versions you're using a std::list instead of a std::vector, which penalizes your performance since you're using pointers. Always use std::vector to store pointers.
Why is it a type?

In what cases the second version will work and when the first is required?
Last edited on
Aibsr wrote:
Why is it a type?
Look up what typedef does.

The second version will never work, the first is always required.
Last edited on
Will the second work if the list is from type <ClassX> and not <ClassX*>?
No, the second will never work because you only have the type and no variable of that type.
Ok. Thanks
Topic archived. No new replies allowed.