cppaddict said:
I am trying to create a pointer, initialize it to NULL or 0, and then
change it's value to an actual value (in this case, a string value).
You can do that, but then you first have to make a string that the
pointer points to. If you write:
std::string* str;
you have a pointer, nothing more. There is no string.
I can't get it work, so I was trying to create a simple example to
pinpoint my problem, and came up with this.
However, even when I use new to initialize the pointer, I still cannot
set it to 0 and then do an insert on it after that.
What do you mean by new to initialize and set it to 0? If you overwrite
the pointer with 0, the string object that you created with new is lost
forever. The pointer again points to nothing and you must not use it.
You can do it the other way round:
std::string* str = 0;
str = new std::string("test");
std::cout << *str;
delete str;
But I still fail to see why you're fiddling around with pointers at all.
Just write:
std::string str;
str = "test";
std::cout << str;
Is there a way to do what I'm trying to do: initialize to NULL and
then chage the value to something else?=
Yes, but you did it the other way round. You initialized it with the
result from new and then overwrote it with 0.