-1

unfortunately, I can't use std::vector and have to use plain C++ arrays. I got the following code:

class Base
{

}

class DerivedCar : Base
{
public:
    DerivedCar(int a) a(a) {};
private:
    int a;
}

class DerivedHouse : Base
{
  public:
    DerivedHouse(float b) b(b) {};
private:
    float b;  
}

class Vector
{
    Vector() :
    index(0)

    void add(const DerivedCar& car)
    {
       vec[index] = new DerivedCar(car.a);
       index++;
    }

    void add(const DerivedHouse& house)
    {
       vec[index] = new DerivedHouse(house.b);
       index++;
    }

private:
    Vector vec[100];
    int index;
}

int main()
{
    Vector vector;
    DerivedCar car(100);
    DerivedHouse house(2.f);

    vector.add(car);
    vector.add(house);
}

I would like to have an array of type Base and add objects of a derived type. Is there a better approach to this other than the way I did? What would be the best way to keep copying of objects at a minimum.

DummySenior
  • 103
  • 1
  • 6
  • 3
    Why does `Vector` (not to be confused with `std::vector`) have an array of itself inside of itself? Do you mean `Base* vec[N]`? – tadman Sep 12 '18 at 18:32
  • Use an array of pointers to base. That'll work. – Jesper Juhl Sep 12 '18 at 18:32
  • If you must avoid `std::vector`, at least implement the same API. `push_back` not `add`. – tadman Sep 12 '18 at 18:33
  • 1
    `class DerivedCar : Base` - you almost certainly want `class DerivedCar : public Base` and similar elsewhere. –  Sep 12 '18 at 18:33
  • @NeilButterworth completely agree, this is fantasy code regardless. At least the desired intent is stated, even if the code is nowhere close to valid C++. – WhozCraig Sep 12 '18 at 18:36
  • 3
    Arrays by definitions may only contain objects of the same type. In order to overcome this you will need an extra layer of indirection, for example by storing pointers to (polymorphic) base class (as suggested above) or some sort of `variant` combining all the types to be stored. – user7860670 Sep 12 '18 at 18:42
  • Why don't you simply use different arrays for different types? – Jesper Juhl Sep 12 '18 at 18:49
  • Vector can't contain an array of Vector. That would make it infinity large because each of these Vectors in the array would have to have an array of Vectors.. – drescherjm Sep 12 '18 at 18:59

2 Answers2

1

How to add derived class objects to an array of base class type?

You can not put derived class objects into raw array or std::vector of base class because the derived class objects are usually larger and so simply do not fit there.

Is there a better approach to this other than the way I did?

Better approaches are certainly out there. One good example of such containers with polymorphic elements is boost::base_collection. Read its documentation and its source code. If you do not understand some detail in it then ask about that detail in Stack Overflow.

What would be the best way to keep copying of objects at a minimum.

Containers that only contain pointers to objects and intrusive containers keep copying of objects at minimum. However such containers do not manage the objects and so responsibility of objects life time has to be taken by something outside.

Öö Tiib
  • 10,809
  • 25
  • 44
0

Here is a possible way that implements a linked list:

class Base
{

};

class DerivedCar : public Base
{
public:
    DerivedCar(int a) { _a = a; };
private:
    int _a;
};

class DerivedHouse : public Base 
{
public:
    DerivedHouse(float b) { _b = b; };
private:
    float _b;
};

class Object
{
public:
    const Base *data;
    const Object *next; 
};

class Vector 
{
public:
    void add(const Base& v)
    {
        Object item;
        item.data = &v;
        head.next = &item;
        index++;
    }
private:
    Object head;
    int index = 0;
};

int main()
{
    Vector vector;
    DerivedCar car(100);
    DerivedHouse house(2.f);
    vector.add(car);
    vector.add(house);
}
jackw11111
  • 1,457
  • 1
  • 17
  • 34