0

I've learned to store derived class pointers in base class vectors by storing base class pointers:

vector<base*> base_vector;
base_vector.push_back(new derived());
// free memory at the end

But if I have an abstract base class:

class interface {
public:
    virtual interface(){} 
    virtual ~interface(){}
};

From which two more abstract classes are derived.

class abstract_derived_1 : virtual public interface 
{ 
public:
    virtual abstract_derived_1(){} 
    virtual ~abstract_derived_1(){}
};

class abstract_derived_2 : virtual public interface 
{ 
public:
    virtual abstract_derived_2(){} 
    virtual ~abstract_derived_2(){}
};

And several other derived classes from the secondary abstract classes:

class derived_1 : virtual public interface, virtual public abstract_derived_1
{
private:
    double value;
public:
    derived_1(){value=0;}
    derived_1(const double val1, const double val2) { value = val1+val2; }
    ~derived_1(){}
};


class derived_2 : virtual public interface, virtual public abstract_derived_2
{
private:
    string name;
public:
    derived_2(){name="";}
    derived_2(string my_str) { name = my_str; }
};

Is it possible to store all of them in a polymorphic vector? As usual, I did the following:

vector<abstract_derived_1*> abs1;
vector<abstract_derived_2*> abs2;
abs1.push_back(new derived_1(1,2));
abs2.push_back(new derived_2("polymorphism"));

But how do I store the two polymorphic vectors in a base class vector?

vector</* What should I put in here? */> interface_vector;
b._.rett
  • 99
  • 9
  • 1
    Why are your constructors `virtual` and how do you manage to assign `derived_2 *` to `abstract_derived_1 *`? – Killzone Kid Apr 07 '18 at 14:06
  • I used virtual so that i have them as pure abstract classes. I’ve made a typo there I’ve now corrected it:) – b._.rett Apr 07 '18 at 14:11
  • 1
    Have you tried to compile your code or no need because you just confident that you can have virtual constructors? https://ideone.com/C8Ii77 – Killzone Kid Apr 07 '18 at 14:18
  • @KillzoneKid I’ve compiled my codes (similar to the simplified example i used above), and everything worked fine, i was able to print out information. But I’m a bit confused about how to store data in polymorphic arrays/vectors :) – b._.rett Apr 07 '18 at 14:20
  • @Z.Yang "*I used virtual so that i have them as pure abstract classes*" - what you have are not abstract classes. That requires methods be declared with not only `virtual` but also `= 0` as well, eg: `virtual void doSomething() = 0;`. And constructors can't be `virtual`, but destructors can (and should be, in a base class) – Remy Lebeau Apr 07 '18 at 18:50

2 Answers2

1

There is no problem to just push_back new instances of derived_1 and derived_2 to a generic vector vector<interface*>, because they have the interface class as ancestor.

By the way: you don't need the derived_1 class and derived_2 class to inherit from interface again. This is a uncommon and I am quite sure that this may lead to other problems.

tangoal
  • 724
  • 1
  • 9
  • 28
0
vector<interface*> interface_vector;
// Loop through abs1 with an index of i
interface_vector.push_back(dynamic_cast<interface*>(abs1[i]));
// Loop through abs2 with an index of i
interface_vector.push_back(dynamic_cast<interface*>(abs2[i]));

Just add the loop above. The main point is that you're able to cast to interface* which is what your vector<interface*> expects.

wes
  • 1
  • 2
  • Thanks for your reply! Could you explain a bit more on dynamic_cast? Would it be the same if I did interface_vector.push_back(abs1[i]) over the size of abs1? – b._.rett Apr 07 '18 at 14:25
  • 2
    The dynamic cast is not necessary. – tangoal Apr 07 '18 at 14:30
  • @Z.Yang I think you will get a compile time error if you do `interface_vector.push_back(abs1[i])` because abs1[i] is `abstract_derived_1*` while interface_vector expects `interface*`, therefore you have to explicitly cast. You may read more about casting here: https://stackoverflow.com/questions/28002/regular-cast-vs-static-cast-vs-dynamic-cast. – wes Apr 07 '18 at 14:38