Reputation: 3
Part from my code:
class Menuitem
{
private:
char* text;
friend class Menu;
};
class Menu
{
private:
char* title;
Menuitem* items;
int identation = 0;
int amount = 0;
};
If I will do delete[] items
, will it also safely delete text
that saved in items
? Or do I need to delete them separetely?
Upvotes: 0
Views: 177
Reputation: 206567
If I will do
delete[] items
, will it also safely delete text that saved initems
?
Not with the posted code.
Or do I need to
delete
them separetely?
Yes.
If you are attempting to learn how memory allcoation/deallocation works, it's good to understand the issues and learn how to use new
and delete
properly.
It is essential to know about The Rule of Three and adhere to it when you are managing dynamic memory.
If you are trying to get an application to working condition, it will be better to avoid using raw pointers. Use containers from the standard library.
class Menuitem
{
private:
std::string text; // No char*
friend class Menu;
};
class Menu
{
private:
std::string title; // No char*
std::vector<Menuitem> items; // No MenuItem*
int identation = 0;
int amount = 0;
};
Then, you have fewer problems to worry about in application code.
Upvotes: 3