Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ vector with inheritance

Reference code:

#include <vector>
#include <iostream>

class Func {
public:
    virtual void call() {
        std::cout<< "Func -> call()" << std::endl;
    }
};

class Foo : public Func {
public:
    void call() {
        std::cout<< "Foo -> call()" << std::endl;
    }
};

class Bar : public Func {
public:
    void call() {
        std::cout<< "Bar -> call()" << std::endl;
    }
};

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

    functors.push_back( Func() );
    functors.push_back( Foo() );
    functors.push_back( Bar() );

    std::vector<Func>::iterator iter;
    for (iter = functors.begin(); iter != functors.end(); ++iter)
        (*iter).call();
}

When run that code, it produces the following output on my computer:

$ ./test
Func -> call()
Func -> call()
Func -> call()

Would there be any way to ensure that the correct virtual function is called in this instance? I'm new at C++ but my best guess is that here:

(*iter).call();

It's being cast to a Func object. Is this correct?

like image 356
Ron E Avatar asked Apr 14 '12 15:04

Ron E


1 Answers

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();
}
like image 154
Andrew Tomazos Avatar answered Sep 27 '22 20:09

Andrew Tomazos