为类'CMatrix'实现运算符'[]'

时间:2015-05-03 05:30:42

标签: overloading operator-keyword

 #include<iostream>

 using namespace std;

 class Array {  
   private:

       int * data;
       int size;
       int isValidIndex();
   public:   
        Array ( )
       {  
          data = 0;   
          size = 0;   
       }   
       Array ( int );   
       Array (const Array &);
       Array & operator = ( const Array & );
       int & operator [] ( int );
       void reSize(int);
       ~Array();
 };


class CMatrix
          {
 private:   
    int * * data;    
    int row;    
    int col;    
    //isValidBounds( int , int );    
 public:    
    CMatrix ( )    
    {    
       data = 0;    
       row = col = 0;    
     }
    CMatrix ( int r, int c );    
    CMatrix ( const CMatrix & );    
    CMatrix & operator = ( const CMatrix & );    

    //many other functions

     };

//你觉得适合为类'CMatrix'实现运算符'[]',这样我们就可以按如下方式操作它们吗?如果是,则为'CMatrix'给出运算符'[]'的定义,否则给出原因(固定/精确定位),以便不实现此运算符。

    main()            
    {
    CMatrix m(3,4); //3 by 4 matrix where ist row/col index is 0    
    m[1][2] = 67;    
    cout << m[1][2]; //should display 67        
    }    

0 个答案:

没有答案