|
|
Suppose, that we want to create ten integer cells to be allocated dynamically. The following declaration will be sufficient:
#include <Block.h>Block<int> b(10);
If the possibility of heap exhaustion is a concern, the client can check to see that the constructor has succeeded by calling the function size(), which would normally return 10:
Block<int> b(10); if(b.size()==0)error();
Although we have said that Blocks are essentially arrays, Blocks do have a few extra ``bells and whistles,'' and size() is one of them. It frees your application from having to remember how big each array is by carrying along an extra integer with each array; to find out how big a Block is, just use the size() operation. But size() serves a dual purpose: clients can use it to ascertain the success or failure of any Block operation that does dynamic memory allocation. Those operations signal failure in exactly the same way as the constructor: by setting the size of the result Block to zero.