template // 基类模板
class array { // (来自条款13)
public:
array(int lowbound, int highbound);
~array();
private:
vector data;
size_t size;
int lbound, hbound;
};
template
class namedarray: public array {
public:
namedarray(int lowbound, int highbound, const string& name);
...
private:
string arrayname;
};
如果在应用程序的某个地方你将指向namedarray类型的指针转换成了array类型的指针,然后用delete来删除array指针,那你就会立即掉进“不确定行为”的陷阱中。
namedarray *pna =
new namedarray(10, 20, "impending doom");
array *pa;
...
pa = pna; // namedarray* -> array*
...
delete pa;