Calling member functions of an object stored in a vector
Solution 1
std::vector::operator[] returns a reference to the object in std::vector. As your objects
stores a pointer to A
, you should call it:
objects[2]->foo();
Now I want to create a loop, in which every object stored in the vector would call it's own foo() function. How should I do this
The easiest loop is:
for (int i=0; i<objects.size(); ++i)
{
objects[i]->foo();
}
use C++11 for loop:
for(auto ep : objects)
{
ep->foo();
}
Or for_each(need to write a small lambda though)
for_each(objects.begin(), objects.end(), [](A* ep){ep->foo(); });
Side note: In practice you'd better store value in STL container.
std::vector<A> objects;
for(auto e : objects)
{
ep.foo(); // call member function by "."
}
If you want to store pointer with STL container, better use smart pointer, e.g.
#include <memory>
std::vector<std::unique_ptr<A> > objects;
objects.push_back(new A());
objects.push_back(new A());
objects.clear(); // all dynamically allocated pointer elements will be de-allocated automatically
Solution 2
You can do this:
std::vector<A> objects;
A b;
A c;
A d;
objects.push_back(b);
objects.push_back(c);
objects.push_back(d);
objects[2].foo();
Please be a bit more specific about the exact error. I suspect maybe the whole problem was trying to reference a pointer to an object with "." instead of "->".
But yes, in general:
1) You can save an object, or a pointer to an object, in any STL container
2) You can call any public method of that object upon accessing it from the container.
Solution 3
You have created pointers to class A and not instances. So, you ought to access the method foo with pointer syntax. i.e
for(int i = 0; i < 3; i++) {
objects[i]->foo();
}
Solution 4
In C++98 and C++03 (Since the other answers already tell you how to do it in the new standard) you can use std::for_each and the std::mem_fun adapter:
std::for_each(objects.begin(), objects.end(), std::mem_fun(&A::foo));
The std::for_each applies a function or functor to each of the elements in the range.
The std::mem_fun adapter takes a pointer to a function in its constructor and a pointer to an object as its (first) argument for operator().
This will result in the std::for_each invoking the foo() member function on each element on the array. If your vector had values on it instead of pointer you can use std::mem_fun_ref instead.
As for the error you are getting, remember that your vector is a collection of pointers so you should invoke the functions as follows:
objects[index]->foo();
Andre Popa
Updated on June 14, 2022Comments
-
Andre Popa almost 2 years
This may sound like a newbie question. How do I call a member function of an object stored in a vector ? Supposedly, I have this class:
class A { public: void foo() { std::cout << "Hello World"; } };
Then I store some objects in a vector:
std::vector<A*> objects; A* b; A* c; A* d; objects.push_back(b); objects.push_back(c); objects.push_back(d);
Now I want to create a loop, in which every object stored in the vector would call it's own foo() function. How should I do this ? At first I thought I could just do something like this:
objects[2].foo();
But it seems that I can't do it like this.