Search code examples
c++deep-copystrcpy

Alternative of strcpy in c++


In C i used strcpy to make a deep copy of a string, but is it still 'fine' to use strcpy in C++ or are there better alternatives which i should use instead ?


Solution

  • In C++ the easiest way is usually to use the std::string class instead of char*.

    #include <string>
    ...
      std::string a = "Hello.";
      std::string b;
      b = a;
    

    The line "b = a;" does the same thing you would usually do with strcpy.