fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class A {
  5. public:
  6. A(std::string value)
  7. : m_value(value)
  8. {
  9. }
  10.  
  11. // A a = std::string("some value")
  12. A& operator=(const std::string value) {
  13. m_value = value;
  14. }
  15.  
  16. operator std::string() const { return m_value; }
  17. const std::string* operator->()const { return & m_value; }
  18.  
  19. private:
  20. std::string m_value;
  21. };
  22.  
  23.  
  24. int main() {
  25. A a("foo");
  26. std::string temp = a;
  27. printf("1.) Temp's value: %s \n", temp.c_str());
  28. printf("2.) A's value w/ static_cast: %s \n", static_cast<string>(a).c_str());
  29. printf("3.) A's value w/ arrow: %s \n", a->c_str());
  30. return 0;
  31. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
1.) Temp's value: foo 
2.) A's value w/ static_cast: foo 
3.) A's value w/ arrow: foo