char *buffe开发者_如何转开发r1 = "abc";
const char *buffer2 = (const char*) buffer;
std :: string str (buffer2);
This works, but I want to declare the std::string object i.e. str, once and use it many times to store different const char*.
What's the way out?
You can just re-assign:
const char *buf1 = "abc";
const char *buf2 = "def";
std::string str(buf1);
str = buf2; // Calls str.operator=(const char *)
Ah well, as I commented above, found the answer soon after posting the question :doh:
const char* g;
g = (const char*)buffer;
std :: string str;
str.append (g);
So, I can call append() function as many times (after using the clear()) as I want on the same object with "const char *".
Though the "push_back" function won't work in place of "append".
str
is actually copying the characters from buffer2
, so it is not connected in any way.
If you want it to have another value, you just assign a new one
str = "Hello";
Make a Class say MyString which compose String buffer. Have a constant of that class. and then u can reassign the value of the composed string buffer, while using the same constant.
精彩评论