Effective C++ Item 16 Use the same form in corresponding uses of new and delete

1106 ワード

1. When you created an array and want to return the memory to system. You need to explicitly add [] to show that that's an array you want to return, so more than one destructors will be called.
2. However, things might not be that simple. Imagine you are fixing a bug and trying to delete an object someone else wrote, for example
std::string *foo = new Foo;

How will you delete it? Pretty much everyone would write
detele foo;

But that is not necessary right until you look a little bit further and come across this line:
typedef std::string Foo[4];

Oops, Foo is actually an array of string, so you gonna need to write
delete foo[];

So, what lesson have you learned from here? 
One is that don't typedef array. And the other is to use STL library instead of dynamic allocated memory unless it's absolutly necessary.