7

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?

chrisaycock
  • 36,470
  • 14
  • 88
  • 125
Ron E
  • 2,214
  • 2
  • 16
  • 14
  • possible duplicate of [Store two classes with the same base class in a std::vector](http://stackoverflow.com/questions/8777724/store-two-classes-with-the-same-base-class-in-a-stdvector) – Bo Persson Apr 14 '12 at 16:18
  • yes, it is however I couldn't find that post before I created this one, sorry my search-fu must not be as good as yours sir – Ron E Apr 14 '12 at 20:19
  • There is object slicing happening in your case, So the derived part is chipped off. You should be using Pointers of based class for Heterogenious Object Storage. – Naveen Aug 23 '12 at 06:39

7 Answers7

7

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();
}
Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319
  • I can't seem to get this code to compile, is this boost code or std c++? my compilers: `i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.9.00) Apple clang version 3.1 (tags/Apple/clang-318.0.58) (based on LLVM 3.1svn)` – Ron E Apr 14 '12 at 20:19
  • @RonElliott: Sorry the code above is using the current C++ standard (2011). I'll write a version with old standard, hang on. – Andrew Tomazos Apr 14 '12 at 20:57
  • @RonElliott: Ok update uses C++03+TR1, you should be able to use that with g++-4.2. Let me know if it still doesn't work. – Andrew Tomazos Apr 14 '12 at 21:00
3

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);
lurscher
  • 25,930
  • 29
  • 122
  • 185
  • This is working, yes I can be sure this vector won't be passed around so that is how I planned to implement – Ron E Apr 14 '12 at 20:23
1

your std::vector is storing Func objects - this means that when you call

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

you're creating Foo and Bar objects, then "slicing" those objects as they are copied into Func objects.

If you'd like to use Foo and Bar polymorphically, then a more typical pattern would be to store a vector of some pointer type (Preferably not "raw" pointers though), for example

std::vector< std::unique_ptr<Func> >

std::vector< std::shared_ptr<Func> >

Or, if you really have to.. (But only if you're using an older compiler which doesn't have shared_ptr or unique_ptr)

std::vector< Func* >

Ben Cottrell
  • 5,741
  • 1
  • 27
  • 34
1

In C++ polymorphism works only with pointers and references, while the vector stores directly instances of objects. When you call push_back the copy constructor of Func is called, which builds the Func object that is stored inside the vector.

This is called object slicing, you can learn more about it with a quick search in StackOverflow.

The solution would be to store pointers (or, even better, smart pointers) to your objects, which should be allocated elsewhere (probably on the heap).

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
1

In general, instances of subclasses may be larger than those of their superclass, so you should not expect the subclasses to fit into your vector's slot.

And push_back probably will call internally a copy constructor (of the Func class, since you have vector<Func>) so the internal vector slots are indeed Func not of some other classes.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

Your problem is that you have a vector of Func, but methods are called polymorphically only through references or pointers.

zvrba
  • 24,186
  • 3
  • 55
  • 65
0

there is a very simple way of making vector without using pointers by creating child class which will inherit Class A and Class B

#include <iostream>
#include <vector>

using namespace std;

struct BASE {
    string name = "Base class ";
};

struct A : BASE {
     string a = " class a ";
};

struct B : BASE {
     string b = " class b ";
};

struct C : A, B{
    //using base name from A or B// yeah is hardcoded
    string name = A::name;
};

int main()
{

    C c;
    c.a = "s";
    c.b = "h";
    vector<C> vec;

    vec.push_back(c);
    c.b = "j";
    vec.push_back(c);

    cout << vec[0].a << endl;
    cout << vec[0].b << endl;
    cout << vec[0].name << endl;

    cout << vec[1].a << endl;
    cout << vec[1].b << endl;
    cout << vec[1].name << endl;

    return 0;
}
maxim
  • 31
  • 2