fork download
  1. #include <iostream>
  2.  
  3. class List
  4. {
  5. List *nextnode;
  6. int val;
  7. int del;
  8.  
  9. public:
  10. List () : nextnode ( NULL ), val ( 0 ), del ( 0 ) {}
  11. List ( int v ) : nextnode ( NULL ), val ( v ), del ( 0 ) {}
  12. ~List ();
  13. List* operator >> ( List* o );
  14. List* operator >> ( int v );
  15. List* operator = ( int v );
  16. operator int ();
  17. int value () const;
  18. List* next () const;
  19. };
  20.  
  21. int main ()
  22. {
  23. List obj = 1;
  24.  
  25. obj >> 2;
  26. obj >> 3;
  27.  
  28. for ( List *p = &obj; p; p = p->next() ) {
  29. std::cout << (int)*p << std::endl;
  30. }
  31.  
  32. return 0;
  33. }
  34.  
  35. List::~List ()
  36. {
  37. if ( del ) {
  38. delete nextnode;
  39. }
  40. }
  41.  
  42. List* List::operator >> ( List* o )
  43. {
  44. if ( nextnode ) {
  45. nextnode->operator >>( o );
  46. } else {
  47. nextnode = o;
  48. }
  49. return this;
  50. }
  51.  
  52. List* List::operator >> ( int v )
  53. {
  54. List *tmp = new List( v );
  55. tmp->del = 1;
  56. this->operator >> ( tmp );
  57. return this;
  58. }
  59.  
  60. List* List::operator = ( int v )
  61. {
  62. val = v;
  63. return this;
  64. }
  65.  
  66. List::operator int ()
  67. {
  68. return val;
  69. }
  70.  
  71. int List::value () const
  72. {
  73. return val;
  74. }
  75.  
  76. List* List::next () const
  77. {
  78. return nextnode;
  79. }
  80.  
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
1
2
3