Jack M
Jack M

Reputation: 5995

How do I initialize an element of an array of superclasses with an instance of a subclass in C++?

Say I have

Animal animals[100];

And now, at run-time, I want to initialize one of these to be a new instance of Cat, a subclass of Animal. How do I do this? I thought maybe I could do

animals[0] = Cat("Fluffy")

but this doesn't seem to work. My understanding of how a constructor works in C++ is that space for the object is allocated, and then a pointer to that space is passed to the constructor function (as this). In particular, the constructor works even if that space contains any arbitrary garbage data. So it seems to me that even if animals[0] already contains data initialized by the constructor of Animal or whatever else was occupying that slot beforehand, it should be possible to just call the constructor of Cat on that space and have it work exactly as if it were a totally "fresh" object. How can I achieve this?

For example, the following code should print "Fluffy", but it prints "Anonymous".

#include <stdio.h>

class Animal
{
public:
    virtual char const *get_name() { return "Anonymous"; };
};

class Cat : public Animal
{
    char const *name;
public:
    Cat(char const *name) { this->name = name; }
    char const *get_name() { return name; }
};

Animal animals[100];

int main()
{
    animals[0] = Cat("Fluffy");
    printf("%s\n", animals[0].get_name());
    return 0;
}

Upvotes: 2

Views: 252

Answers (2)

jtbandes
jtbandes

Reputation: 118691

This is wrong because animals[0] = Cat("Fluffy") calls Animal::operator=(const Animal&), which forgets about all the Cat-specific parts of the object. Poor Fluffy has been sliced 😭

Generally if you want a heterogeneous array, you would need to store an array of pointers to the base class, or preferably a smart pointer, e.g.:

std::unique_ptr<Animal> animals[100];
animals[0] = std::make_unique<Cat>("Fluffy");

You could also use something like std::variant.

This is necessary because subclasses may have different sizes. For example, in your case, Cat contains a name pointer, but Animal does not, so a Cat will not fit in space that was allocated only for an Animal. To work around this, you will either need indirection provided by a pointer, or some kind of union to ensure enough space is available inline. (std::variant can do this, or std::aligned_union + placement new for a no-batteries-included approach).

If you're storing multiple classes derived from the same base, a union will not allow you to use the common base interface, while a pointer will, which is why I'd recommend a pointer here. Remember to give your base class a virtual destructor.

See also:

Upvotes: 3

Joseph Larson
Joseph Larson

Reputation: 9058

You flat out can't do it the way you're trying to. When you create that array, you allocate exactly enough space to hold 100 Animals. Well, Cats might be a bigger object. There isn't space anymore.

Like some of the other comments, you would need to use a different data structure, probably one that is pointer-based.

Upvotes: 2

Related Questions