IntegrateThis
IntegrateThis

Reputation: 962

De-Referencing each element of an std::vector of pointers to objects

Supposing that I have a class called TextEntry with some instance variables and a method ask() (which returns an integer).

I then create a vector of type: std::vector<TextEntry*> d_text (a vector of pointers of type TextEntry). I wish to iterate over all the elements in this vector and call the method ask() applied to each element. Is this the correct way to do it?

for (std::vector<TextEntry*>::iterator it = d_text.begin(); it != d_text.end(); ++it) {

    TextEntry* TextEntryPointer = *it; // first dereference the iterator 

    TextEntry TextEntry = *TextEntryPointer; // dereference the TextEntry pointer

    int j = TextEntry.ask();

}

For whatever reason this implementation is giving me an error so it would be helpful to know if the reason why is because of the above code or some other problem in the rest of my project.

Upvotes: 0

Views: 49

Answers (3)

eerorika
eerorika

Reputation: 238401

I wish to iterate over all the elements in this vector and call the method ask() applied to each element. Is this the correct way to do it?

Almost.

Your program doesn't call ask on any element pointed by d_text but on a copy of each pointed element.


I am confused why it is a copy.

You create a variable of type TextEntry. You copy-initialize it from *TextEntryPointer. That is why there is a copy.

Is there a way to call it on the original element?

Yes. Instead of copy-constructing a TextEntry variable, you could use a reference variable instead:

TextEntry& TextEntry = *TextEntryPointer;

This refers to the object pointed by the element in the vector.

Or more simply, don't create an intermediate variable at all:

int j = TextEntryPointer->ask();

Perhaps not even for the pointer:

int j = (*it)->ask();

You could simplify the loop as well:

for(TextEntry* ptr : d_text) {
    int j = ptr->ask();
}

For whatever reason this implementation is giving me an error

There is a missing semicolon

TextEntry TextEntry = *TextEntryPointer // dereference the TextEntry pointer

PS. Avoid naming a variable with a same identifier as a type.

Upvotes: 3

Lehu
Lehu

Reputation: 761

Other answers are correct. But we have 2016 now, so we have c++11:

for (auto& text_entry_ptr: d_text)
{
    int j = text_entry_ptr->ask();
}

Much simpler and cleaner.

Upvotes: 1

Pavan Chandaka
Pavan Chandaka

Reputation: 12781

An example program with little bit assumption. Look into main function for invoking the member function.

 #include <iostream>
    #include <vector>

using namespace std;

class TextEntry
{
    public:

    int ask()
    {

       return 1;

    }

};

int main()
{
    vector<TextEntry*> te;

    TextEntry* te1 = new TextEntry();
    TextEntry* te2 = new TextEntry();
    TextEntry* te3 = new TextEntry();
    TextEntry* te4 = new TextEntry();

    te.push_back(te1);
    te.push_back(te2);
    te.push_back(te3);
    te.push_back(te4);

    for (std::vector<TextEntry*>::iterator it = te.begin(); it != te.end(); ++it) 
    {
        TextEntry *t = *it;
        cout<<t->ask();

    }

}

Upvotes: 1

Related Questions