char       *buffer1 = "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";
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With