#include <string>
#include <iostream>
usingnamespace std;
int main(){
string* str1;
str1->reserve(113);
if(true)
{
string temp("I am alive now,but soon i will be dead");
*str1= temp; //i guess here the problem are.
}
cout<<str1;
}
i got a run-time error when i ran it ,and i feel weird about that.
First leat me analysis this statement *str1= temp;,it invokes the
operator = function defined(which i believe is well implemented) within the
string class and assign the data string temp refers to to the string
I think the problem is str1->reserve(113);, because str1 is not initialized before using it.
Try this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
string* str1 = new string();
str1->reserve(113);
if(true)
{
string temp("I am alive now,but soon i will be dead");
*str1= temp; //i guess here the problem are.
}
cout<<*str1;
if (str1)
{
delete str1;
}
That, unless you explicity set a pointer to 0, it will never point to 0 by default. So the if statement is not only redundant, but useless, because if you WERE to set it to 0, trying to delete it would have no effect.