18

I am struggeling with an STL list that holds Pointers of my "Object" object.

I declared:

list<Object*> objectlist;

and inserted via:

this->objectlist.push_back(new Object(address,value,profit));

and tried to iterate like in maps and others:

list<Object*>::iterator iter;

iter = this->objectlist.begin();

while(iter != this->objectlist.end())
{
    iter->print();
}

Where print() is a public Method of class Object;

Whats wrong here?

I cannot access via iterator to the objects in the list ?

7 Answers 7

40

You need (*iter)->print();

Since you have an iterator to a pointer, you have to first de-reference the iterator (which gets you the Object*) then the arrow de-references the Object * and allows the call to print.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, your iterator is not a pointer to your object type, but an iterator. Also don't forget your increment or you will have an infinite loop.
7

You are not incrementing your iterator! Change your while loop to a for loop like so:

for (list<Object*>::const_iterator iter = this->objectlist.begin(),
     end = this->objectlist.end();
     iter != end;
     ++iter)
{
    (*iter)->print();
}

(Also iter dereferences to a pointer, like the other answers have pointed out.)

Comments

3

You can access the value pointed by iterator with *iter

Also, remember to increment the iterator in each iteration. Otherwise you get stuck in an endless loop.

Like this:

iter = this->objectlist.begin();

while(iter != this->objectlist.end())
{
    (*iter)->print();
    iter++;
}

Comments

2

You could try the C++11 way (if possible). The dereference of the iterator comes free :)

for (auto& iter: this->objectlist)
{
     iter->print();
}

Also if print() is a const function you should use

for (const auto& iter: this->objectlist)

1 Comment

2 questions: - "i" should be "iter", right? - is the "&" needed? Seems to work fine without it too
1

Instead of the for-loop you should use STL's for_each() - see the advantages of for_each over a simple for-loop.

Syntax

#include <algorithm> // 'for_each()' is in there

std::for_each(InputIterator first, // Iterator start position
              InputIterator last,  // Iterator end position
              Function fn);        // Unary function - executed on all elements in range

for_each example

Here's how your example looks std::for_each() instead of for( ... ):

list<Object*> objectlist;

// insert some elements …

// Deletes all elements of 'objectlist'
std::for_each(objectlist.begin(), objectlist.end(), DeleteObj<Object*>());

Unary function

The unary function is implemented using a template, so you can use it with any type. Just make sure T is a pointer type!

template<class T> class DeleteObj
{
public:
    bool operator()(T obj) const
    {
        delete obj;
        return true;
    }
};

Btw. you don't have to implement this as template. And: you can bind a member function instead of such a implementation too!

Documentation

Comments

0

you should use the following way:

(*iter)->print()

1 Comment

Something more to say: *iter means the element, so you should first get the element, and then to use it.
0

An iterator references an element in the list, so for a list of type T, dereferencing an iterator yields a value of type T.

In your case, T is actually a pointer - Object*. Hence, the iter-> call (which dereferences the iterator) yields a pointer. You have to dereference that pointer to get to the actuala object.

Try using

(*iter)->print()

instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.