0

The thing is like this, I have a base class, and I implemented the derived class in two different ways, but the calling method of these two derived class object is same, I would like to push these two derived classes objects into a vector so that I could call them by their index:

class Base {
    public: 
        virtual int operator()()=0;
};

class Derived1: public Base {
    public:
        int operator()() override {
            return 1;
        }
};

class Derived2: public Base {
    public:
        int operator()() override {
            return 2;
        }
};

int main(void)
{

    vector<Base> list;
    list.push_back(Derived1());
    list.push_back(Derived2());
    cout << list[0]();


    return 0;
}

But this does not work, since vector will not recognize derived class as the base class. How could I make it work please ?

coin cheung
  • 949
  • 2
  • 10
  • 25
  • You probably want `vector>`. – alter_igel Mar 13 '20 at 02:27
  • Yes, I know that a pointer would let base class ptr to call derived class functions. Is there other better way ? Could I convert it to something like std::function since they looks same when they are called ? – coin cheung Mar 13 '20 at 02:31
  • 1
    Yes, you could in principle have a `vector>` storing a series of lambdas, each of which captures and calls some derived `Base` object, but there will be overhead in terms of performance and boilerplate code. `vector>` will be simpler and probably more efficient too. – alter_igel Mar 13 '20 at 02:36
  • This didn't occur to me until later, but your `Derived1` and `Derived2` classes are actually directly convertible to `std::function`. [Live demo here](http://coliru.stacked-crooked.com/a/bb035c875a52cf25). However, in doing so, you also lose the connection to the `Base` interface and there's no point in inheritance anymore. – alter_igel Mar 13 '20 at 17:54
  • Hi, Do you mean that by using std::function, there will not be concept of base class in the function object stored in vector any more? – coin cheung Apr 07 '20 at 03:23
  • Yes, that's correct. `std::function` will happily store any class that is copyable and implements `operator()`, but it achieves this using type erasure. That means that the same specialization of `std::function` can hold an instance of any class object or function pointer with the appropriate function signature, but in doing so, all useful information about the underlying type is lost. You won't practically be able to tell that a given `std::function` object is holding something that derives from `Base`. – alter_igel Apr 07 '20 at 04:25
  • Thanks for replying !! Is the std::function method less efficient than the pointer method? Is so, is it less efficient in the `push_back` phase or also in the calling phase? – coin cheung Apr 07 '20 at 05:22
  • My advise would be to first write your code so that's easy to read and cleanly-designed. Then, later, if you find a good reason why it's too slow, start thinking about optimizations. "Premature optimization is the root of all evil." – alter_igel Apr 07 '20 at 14:13

0 Answers0