2

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;
}
10
  • 3
    This is wrong because animals[0] = Cat("Fluffy") calls Animal::operator=(const Animal&). stackoverflow.com/questions/274626/what-is-object-slicing Generally if you want a heterogeneous array you would need to store 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. Commented Feb 9, 2021 at 20:58
  • @jtbandes That seems out of sync with how simple what I'm trying to do seems to be, to me. Why do I need pointers? I want my data to be stored sequentially in memory, and for my use case I don't need or want to have to manage allocating and freeing. I don't see why the notion of subclassing should be coupled so tightly with whether you use pointers or direct references. Commented Feb 9, 2021 at 21:01
  • 2
    Well, subclasses may have different sizes: your Cat object contains a name pointer, but Animal does not, so a Cat will not fit in space that was allocated only for an Animal. You will either need indirection provided by a pointer, or some kind of union (ensuring enough space is available; 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. Commented Feb 9, 2021 at 21:05
  • 1
    C++ brutally abuses folk who dare make assumptions about it. In this case, at the time Animal is compiled say only only Cat and Dog exist as subclasses. A while later, Wombat is added in a third-party library. There's no way for Animal to know of this new subclass. Because of this Animal only knows Animal. All derived classes are unknown. Commented Feb 9, 2021 at 21:17
  • 1
    Incidentally this is also why virtual destructors are important: stackoverflow.com/questions/461203/… Commented Feb 9, 2021 at 21:19

2 Answers 2

3

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:

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

Comments

2

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.

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.