In C# all classes are references, which means that under the hood they are actually closer to what C++ would term pointers.
So in this case, you need to be using pointers:
Food * food[1];
Apple * apple = new Apple();
food[0] = apple;
Please bear in mind that if you want to use new
to dynamically allocate pointers then you need to remember to delete
the object afterwards when it’s no longer needed.
If you want behaviour that’s closer to C# then you could use some of the smart pointer types introduced in C++. The one that’s most similar to how C# handles object references is the std::shared_ptr
, which automatically deallocates the object when the last std::shared_ptr
that refers to it is destroyed (i.e. it uses a technique called reference counting).
#include <memory>
std::shared_ptr<Food> food[1];
std::shared_ptr<Apple> apple = std::make_shared<Apple>();
food[0] = apple;
Edit:
When I say ‘destroyed’ here, I mean when it goes out of scope or gets overwritten.
So for std::shared_ptr
, when you’re done with it, you have to do what you would in C# and nullify it by assigning nullptr
to it.
E.g.
food[0] = nullptr;
If you use std::shared_ptr
then you don’t need to use new
or delete
and only have to worry about assigning nullptr
in the same cases that you would in C#. Hence this approach may be more familiar to you.
Be warned though that this approach does come with some additional overhead, as do virtual
functions and polymorphism, so it can sometimes be worth finding alternative approaches.
Be aware that when using either raw pointers or smart pointers, you must use ->
instead of .
to access elements.
E.g.
std::uint32_t price = food[0]->price;
(It took me a moment to realise this was a different problem so I had to move it twice. I’m migrating these questions to separate topics so they’re easier for other people to find if they get the same problem.)