c++ vector with inheritance

You should use a shared_ptr or unique_ptr to hold the elements of a collection of polymorphic type.

As your code is written now the Foo and Bar instances are cooerced (copy constructed) into an instance of type Func to fit into the vector. (The reason is that vector stores its elements immediately by fixed-sized value for performance, however polymorphic subclasses are of a arbitrarily larger size unknown at compile-time, so it can only store the base class.)

This is better:

int main(int argc, char** argv) {
    vector<shared_ptr<Func>> functors;

    functors.push_back( make_shared<Func>() );
    functors.push_back( make_shared<Foo>() );
    functors.push_back( make_shared<Bar>() );

    for (auto functor : functors)
        functor->call();
}

In the above a reference-counted pointer is used to implicitly share the hetrogeneous subclasses of Func in the vector. (This indirection allows arbitrarily sized subclasses of Func to be stored by address indirection.)

Also, you may want to take a look at std::function and std::bind, rather than rolling your own functor type.

Another thing to look at would be perfect forwarding and varadic templates.

update: For old compiler:

int main(int argc, char** argv) {
    vector<std::tr1::shared_ptr<Func> > functors;

    functors.push_back( std::tr1::make_shared<Func>() );
    functors.push_back( std::tr1::make_shared<Foo>() );
    functors.push_back( std::tr1::make_shared<Bar>() );

    for (size_t i = 0; i < functors.size(); ++i)
        functors[i]->call();
}

the vector only holds the type Func by value, meaning that all your temporals Foo and Bar are sliced and casted to the base Func type

you need to change to something like std::vector< Func* > and dynamically allocate the derived classes instances in order for polymorphic dispatch to work

If you are completely sure that you won't pass this vector to other functions after this function returns, as an optimization you might want to allocate the instances in the stack:

std::vector< Func* > v;
Bar b;
Foo f;
v.push_back( &b);
v.push_back( &f);