c++ - g++ gives the warning message deleting array in virtual destructor, what does this mean? -



c++ - g++ gives the warning message deleting array in virtual destructor, what does this mean? -

i have class room inherits abstract class mapsite. destructor room looks this:

room::~room() { delete[] sides; }

mapsites's destructor this:

virtual ~mapsite() {}

the constructor looks this:

room::room() : inventory(new inventory) { for(size_t = 0; < 5; i++) sides[i] == nullptr; }

the private members of room looks this:

private: int roomnumber = invalid_room_number; // room number mapsite *sides[5]; // room roof/floor/wall/exits std::string name; // name of room std::string description; // room description std::string lookdescription; // description shown on command std::string filename = invalid_room_filename; // lua file associated room bool visited = false; std::unique_ptr<inventory> inventory;

and getting warning:

room.cxx: in destructor ‘virtual room::~room()’: room.cxx:45:12: warning: deleting array ‘((room*)this)->room::sides’ [enabled default] delete[] sides;

could please explain me warning means? , should/how can suppress it?i couldn't find quick google search.thanks!

in general shouldn't delete[] array declared using syntax

t a[n];

where t type , n integer.

consider first happens if array allocated on stack (automatic storage duration). automatically deallocated @ end of scope, shouldn't phone call delete on it.

now consider happens if a has dynamic storage duration. can't dynamically allocate array a when declared array type. calling new gives pointer, can't assign pointer value array variable. a must subobject of dynamically allocated object. in case memory a allocated when finish object created, , deallocated when finish object destroyed. 1 time again, no need manually manage memory a.

on other hand, may want delete each element of array sides, since each element pointer. delete[] syntax not this. have write

for (int = 0; < 5; i++) { delete sides[i]; }

c++ memory destructor

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -