If you were to use raw pointers |
I wouldn't ;P. If you are manually cleaning up, you are probably doing something wrong.
But
anyway....
do you think it would be good measure to set the pointers to NULL after de-allocating memory from them, or is that overkill in terms of ensuring safety from undefined behaviour? |
It isn't strictly necessary (especially if done in a destructor)... but it doesn't hurt to do it.
It's really a case of "better safe than sorry" -- so yeah it is generally a good idea to null your pointers after deleting them. That's a good habit to have.
(though a better habit is to use smart pointers and container classes so you don't have to deal with this at all)
In your fill() function declaration, does creating the parameter like that basically mean that you can provide your own value to set the elements of the vector to, but you can also call fill() without passing a value and it will set the elements to '0'? |
Yes. This is an "optional parameter". When calling the function, the parameter is optional, and if omitted, '0' would be used.
1 2 3
|
obj.fill(); // <- fills with '0'
obj.fill('0'); // <- same
obj.fill('#'); // <- fills with '#'
|
Is this a new kind of for loop introduced in C++11? |
Yes. It's a "range based for loop" and it makes traversing arrays and container classes much easier.
It's sort of like the C++ version of the "foreach" construct seen in other languages. In my code example, 'i' would be the next element in the vector each iteration of the loop.
Does this set the number of elements in grid to width * height? |
Yes
Is the additional parameter '0' in this initialisation something unique to vectors? |
No, other container classes have it as well.
string, deque, and list all have it as well. Any container where you can explicitly set the size of it will allow you to provide an initialization value.