Using "->" on a vector

Hi, I'm relativly new to C++ and i am just getting into classes. I was wondering if it's possible to use the arrow operator to call a fuction from another class on an item in a vector.

Example:
(myVector contains a series of strings)

for(unsigned int counter=0; counter<myVector.size(); counter++){
total += myVector.at(counter) -> getPrice();
}

As you can see i am attempting to retrieve the price of an object that will have the same name as the strings contained within the vector. I get the feeling i should try to make a vector of objects but i don't quite know how to because the situation is: there are 2 kinds of objects (= of different classes) that will have to go into the vector, which is itself part of a third class.

Any help would be greatly appreciated.
If it contains a series of strings, you can't do that. It would need to contain pointers to use the -> operator.
no you cannot do that. The vector needs to be an array of pointers to whatever type of class instance has the getPrice function.

You can do this via inheritance if your objects can inherit a common interface class. then you can add pointers to the common base class within the vector.

Take a look at the inheritance tutorial for starters or whatever other resources that are available to you such as your books or other websites.
http://cplusplus.com/doc/tutorial/inheritance/
Last edited on
Topic archived. No new replies allowed.