fork download
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. struct test
  5. {
  6. public:
  7. test(const std::string& str) : name(new char[str.length() + 1])
  8. {
  9. strcpy((char*)name, str.c_str());
  10. }
  11. // Rule of three
  12. // -- Copy ctor
  13. // -- Copy assignment operator
  14. // Rule of five
  15. // -- Move ctor
  16. // -- Move assignment operator
  17. ~test() { delete[] name; }
  18. void printName() { std::cout << name << std::endl; }
  19. private:
  20. const char* name;
  21. };
  22.  
  23.  
  24. int main()
  25. {
  26. std::string s{"test"};
  27. test t{s};
  28. t.printName();
  29. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
test