1. In the constructor
with
itsLength =0 then
pChamber = new T[itsLength] will return a bad ponter, because you
are asking for zero size array.
2. In the destructor
delete pChamber; should be
delete [] pChamber;
Coming back to why you have a problem with
string but not say for example with
int
Consider the following:
(for these examples we will make the pChamber pointer public)
1 2 3 4 5 6 7
|
int main ()
{
chamber<int > cs; // cs will have a rubbish pChamber pointer as we said earlier
cout << cs.pChamber[0] << endl;
return 0;
}
|
The above will work because even if pChamber is a useless pointer really, all an integer is is 4 bytes - nothing special at all.
But:
1 2 3 4 5 6 7
|
int main ()
{
chamber<string > cs; // cs will have a rubbish pChamber pointer as we said earlier
cout << cs.pChamber[0] << endl; //crash
return 0;
}
|
This fails because a string is more complex than a simple int - it is a class - with a pointer member.
So you have a rubbish pointer (pChamber), pointing to unitialized data, which you are trying to
decode as a valid string variable, which it most certainly is not - which means that the pointer member of an invalid string will be itself invalid - hence the problem.