LibertyPaul

8lab.operator[]

May 15th, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.56 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class list{
  5.     public:
  6.     list *next;
  7.     int n;
  8.    
  9.     list(int a=0){
  10.         n=a;
  11.         next=NULL;
  12.     }
  13.    
  14.     void add(int a){
  15.         list *current=this;
  16.         while(current->next!=NULL)
  17.             current=current->next;
  18.         current->next=new list(a);
  19.     }
  20.    
  21.     list *operator[](int a){
  22.         list *current=this;
  23.         for(int i=0;i<a;i++){
  24.             if(current->next!=NULL)
  25.                 current=current->next;
  26.             else break;
  27.         }
  28.         return current;
  29.     }
  30. };
  31. int main(int argc, char **argv){
  32.     list L(5);
  33.     L.add(6);
  34.     L.add(7);
  35.     L.add(42);
  36.     cout<<L[3]->n<<endl;
  37.     return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment