I feel like there's two problems here for you. One is a syntactical problem, which others have already addressed. However, it seems that you also have an underlying problem of trying to write Java/C# code in C++. This will lead to misery no matter what the syntactical problems are, so I try to address this here.
In c++ when I do this with vector it calls that parent's function. How can I do the example above in C++?
Java and C# use the object-oriented paradigm for everything. C++ is different in that C++ is a multi-paradigm language. It supports (more or less) Structured, OO, Generic, Functional and whatnot Programming paradigms. You can freely mix and mingle paradigms, and C++ shines brightest where you do that.
The part of the standard library that derives from the STL, that is: containers, algorithms, iterators, are not OO at all. They are applying Generic Programming. One of the attributes of that is that the containers usually (there are exceptions, but not within the standard library itself) store values, rather than references. Polymorphism, however, at least runtime polymorphism, only operates on references (or, syntactically, pointers, which are, semantically, references, too).
If you have a std::vector<base_class> vc
, this will store actual values, rather than references to objects somewhere on the heap. If you put an object into such a container, the object will actually be copied into the container. If you put in a derived_class
object, then that is subjected to slicing. That is, only the base_class
part of it will be copied into the container, all the derived_class
parts will be disregarded. You then end up with an actual base_class
object in the container, rather than, as in Java and C#, a base class reference to a derived class object somewhere on the heap.
That is why invoking a member function on that object will end up in the base class: there is no derived class object to invoke a function on.
In C++, if you want to employ OOP, you will usually have to dynamically allocate derived class objects (i.e., new derived_class()
) and assign them to base class pointers. The problem with this is that C++ does not have garbage collection, so you must keep track of those pointers, and all the copies made from it, and explicitly delete the object just before the last pointer gets destroyed. That is very error-prone to do manually, which is why nowadays everybody lets smart pointers do this automatically.
So what you want is std::vector<smart_ptr<base_class>>
and put in new derived_class()
objects. What the symbolic smart_ptr
refers to depends on your needs. If you plan to store pointers to those objects nowhere but in that container, std::unique_ptr
(std::tr1::unique_ptr
if your compiler only supports C++03, or boost::unique_ptr
if it doesn't even support that) would be ideal. If you freely pass around such pointers, and them to keep track of when the last goes out of scope for themselves, std::shared_ptr
would be better.
Now, all this said, I feel the need to add: You might not need to do this the OO way at all. There might be a much better design if you could just let go of the rigid OO thinking Java and C# have imprisoned you in.
If you employ polymorphism just so you can pass containers with different content to the same algorithms, then employing Generic Programming might be much better:
template<typename FwdIt>
void do_something(FwdIt begin, FwdIt end)
{
while(begin != end)
if(begin->foo() == bar()) // whatever
begin->baz(); // whatever
}
std::vector<some_class> vs;
std::vector<other_class> vo;
std::deque<other_class> do;
// ...
do_something(vs.begin(), vs.end());
do_something(vo.begin(), vo.end());
do_something(do.begin(), do.end());
This works for all types (here it's some_class
) that have a foo()
member not taking any arguments and returning something comparable with whatever bar()
returns, and have a baz()
member, not taking any arguments either. (If you try to use some type that doesn't have those, the compiler will bark at you.)