fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class strtype {
  6. string str;
  7. public:
  8. strtype() {
  9. cout << "strtype()" << endl;
  10. str = "Test";
  11. }
  12.  
  13. strtype(const strtype &st) {
  14. cout << "strtype(const strtype &)" << endl;
  15. str = st.str;
  16. }
  17.  
  18. strtype(const string &ss) {
  19. cout << "strtype(const string &)" << endl;
  20. str = ss;
  21. }
  22.  
  23. strtype(const char *ss) {
  24. cout << "strtype(const char *)" << endl;
  25. str = ss;
  26. }
  27.  
  28. strtype& operator=(const strtype &st) { // <-- change this!
  29. cout << "operator=(const strtype &)" << endl;
  30. str = st.str;
  31. return *this;
  32. }
  33.  
  34. string get_str() const { return str; };
  35. };
  36.  
  37. int main()
  38. {
  39. strtype b = "example";
  40. cout << b.get_str() << endl;
  41.  
  42. b = "something else";
  43. cout << b.get_str() << endl;
  44.  
  45. return 0;
  46. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
strtype(const char *)
example
strtype(const char *)
operator=(const strtype &)
something else