char *buffer1 = "abc";
const char *buffer2 = (const char*) buffer;
std :: string str (buffer2);
This works, but I want to declare the std::string object ie 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 *)
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";
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".
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.