You can have a vector of pretty much anything, including a vector of pretty much anything.
That includes pointers !
std::vector<Item*> or std::vector<std::shared_ptr<Item>> or std::vector<unsigned> or even std::vector<std::vector<Item*>> …
Some stuff can’t be std::vector'd though, because it requires copying/moving your objects inside the memory it manages (may be best to refer to the doc for that subject). That’ll never be the case for raw pointers!
The reason your code fails is because you’re making the array pointers point to arrays allocated on the stack instead of on the heap, so calling delete[] ends up destroying part of the stack, which in turn upsets the whole program and makes it impossible to continue.
Also your for loop that’s supposed to do the copying won’t work because of the zeros
I’m glad you decided to go with std::vector instead.
Managing dynamically allocated arrays is far from straightforward.
There are so many edge cases that by the time you’re doing it right you’ve ended up rewriting a large chunk of the interior of std::vector, and thus might as well have used it in the first place.
If you want I’ll write an example of ‘doing it right’,
but it would be a very large example,
and my advice would still be “don’t use this, std::vector does the same, but better”.
I’m guessing by ‘some stuff’ you mean objects that have their copy and move constructors explicitly deleted or marked private or `protected?
Thanks, don’t need that anymore. Vectors seem to be pretty nice ^^
But I have a general question:
I use the food vector with pointers as global variable. Do I really need to save pointers in the vector? Since the vector is global its never out of scope.
I’m not entirely sure what you’re thinking,
but if you mean what I think you mean then I think your best bet would be to use a std::vector<std::shared_ptr<Food>>.
It’s slightly more expensive than some other alternatives but it will behave more closely to a C# List<Food> and it means you won’t have any memory leaks or invalid pointers hangining around.
class Test { public int value; }
void test()
{
Test a = new Test();
a.value = 5;
Test b = a; // b now refers to a
b.value = 10;
// a.value is now also 10
}
But in C++:
class Test { public: int value; };
void test()
{
Test a = Test();
a.value = 5;
Test b = a; // b is a copy of a
b.value = 10;
// a.value is still 5
}
To get the same behaviour you have to use other techniques, for example:
class Test { public: int value; };
void test()
{
Test a = Test();
a.value = 5;
Test & b = a; // b is a reference to a
b.value = 10;
// a.value is now 10
}
In C#, pretty much all class creation is done on the heap,
whereas C++ lets you choose where your class is allocated: heap, stack or the global variable area.
By default C++ classes are closer to how C#'s structs work.
Furthermore, child classes sometimes have extra member variables.
Consider this case:
class Parent
{
char a;
}:
class Child : public Parent
{
char b;
};
sizof(Parent) == 1) and sizeof(Child) == 2.
So if you tried to do:
Parent parent = Child();
You’re trying to fit 2 bytes into a 1 byte sized variable.
It just doesn’t work.
@Pharap Your grasp of the subject of C++ and ability to explain in plain language is something I have never seen before. I am not saying this to flatter you, I mean it.
Wow, thank you.
Whether intentional or not I’m flattered all the same.
I get a lot of ‘thanks for the help’, but rarely do I get praise like that.
I have started writing books once or twice but I find it hard to keep going and to keep the tone right (one attempt was called “C++ for pudding brains” and was very tongue-in-cheek :P).
Truthfully I find it easier to explain 1-to-1 becuase I can adapt my explanation to suit the person I’m talking do.
For example, I know @zer0 has C# experience, so in his case I can use C# as a base line to relate the features to.
(Maybe one day when I’ve contributed enough I’ll be worthy of a ‘resident C++ expert’ or ‘resident C++ wizard’ badge.)