-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.

8
  • 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]? Commented Sep 12, 2018 at 18:32
  • Use an array of pointers to base. That'll work. Commented Sep 12, 2018 at 18:32
  • If you must avoid std::vector, at least implement the same API. push_back not add. Commented Sep 12, 2018 at 18:33
  • 1
    class DerivedCar : Base - you almost certainly want class DerivedCar : public Base and similar elsewhere. Commented Sep 12, 2018 at 18:33
  • 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. Commented Sep 12, 2018 at 18:42

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.