重载操作符时,有的操作符(如 <<, >>)用friend来重载可以, 
可不可以作为类的成员来重载。或者说哪些操作符不可以作为类的成员来重载(只能用friend来重载)。

解决方案 »

  1.   

    我感觉除<< , >>
    大部分类的成员来重载
    如: +_*/ == !=
      

  2.   

    . :: * -> ? :好象都不能重载,不论用friend还是作为类的成员!
    剩下的好象都可以作为friend或类的成员被重载!
      

  3.   

    如 yndfcd(YNDFCD) 所说,所有能重载的操作符,都可以作为友元或类成员来重载。
    哪位高手帮忙将 这两个算符(<<, >>)用类成员来重载(该程序运行无误),谢谢。// Matrix.h#include <iostream.h>
    class CMatrix  
    {
    private:
    int Row, Col;
    int *Elem;public:
    CMatrix();
    CMatrix(int row, int col);
        CMatrix(const CMatrix&); virtual ~CMatrix();    CMatrix& operator = (const CMatrix&);
        CMatrix  operator + (const CMatrix&);
        CMatrix  operator - (const CMatrix&);
        CMatrix  operator * (const CMatrix&);    friend ostream& operator << (ostream&, const CMatrix&);
        friend istream& operator >> (istream&, CMatrix&);
    };
    // Matrix.cpp: implementation of the CMatrix class.#include "Matrix.h"CMatrix::CMatrix()
    {
    cout << "请输入矩阵的行、列:";
    cin  >> Row >> Col;
    Elem = new int[Row*Col];
    for(int i=0; i<Row*Col; i++)
    {
    Elem[i] = 0;
    }
    }......ostream& operator << (ostream& os, const CMatrix& M)
    {
      for(int i=0; i<M.Row; i++)
      {
        for(int j=0; j<M.Col; j++)
        {
    os << M.Elem[i*M.Col+j] << "\t";
        }
        os<<endl;
      }
      return os;
    }istream& operator >> (istream& is, CMatrix& M)
    {
      for(int i=0; i<M.Row*M.Col; i++)
      {
        is >> M.Elem[i];
      }
      return is;
    }
      

  4.   

    * 和 -> 是可以重载的,如下:
    CMemDC * operator->(){return this;}
    operator CMemDC*(){return this;}“将这两个算符(<<, >>)用类成员来重载”当然是可以的,但就不是做为插入/析取符了。
    重载运算符为类的成员,类必须在算符的左边。
      

  5.   

    friend ostream& operator << (ostream&, const CMatrix&);
        friend istream& operator >> (istream&, CMatrix&);声明为friend是因为该重载操作符后的函数要被其他类的对象调用,例子中就是被ostream的对象调用
      

  6.   

    http://202.113.96.10/ini/c++/flag_chong.htm
      

  7.   

    不知道为什么有这样的要求。作为类成员是可以的。不过看上去可能有点奇怪。第一种方法:
    重载为ostream类的成员
    class myostream: public ostream
    {
    public:
       myostream& operator<<(const CMatix& M)
    {
      for(int i=0; i<M.Row; i++)
        {
          for(int j=0; j<M.Col; j++)
          {
      ostream::operator<<(M.Elem[i*M.Col+j]);
                        ostream::operator<< ("\t");
          }
          ostream::operator<<(endl);
        }
        return *this;
     }
    }
    当然要声明类myostream为CMatrix的友员。
    另一个与些类似。第二种方法看上去更奇怪
    class CMatrix
    {
       .
       .
       .
        ostream& operator<<(ostream& os)
    {  
      for(int i=0; i<M.Row; i++)
        {
          for(int j=0; j<M.Col; j++)
          {
      os<< M.Elem[i*M.Col+j]<<\t";
           }
          os<<endl;
         }
        return os;
    }
    }
    你要这样写代码CMatrix m1, m2, m3, m4;
    m1<<(m2<<(m3<<(m4<<cout))));重载为友员是最好的。后面哪位大侠有更好的方法,请贴上。
      

  8.   

    如果operator <<不用直接使用CMatrix的私有成员变量的话,不申明为友元也是可以的。
      

  9.   

    to:yndfcd(YNDFCD)
    我觉得在你的第二段代码中,用>>更直观一些。即改为:
    ostream & operator >> (ostream & os)
    及:
    m1>>(m2>>(m3>>(m4>>cout)));
    不过这样输出的是m4 m3 m2 m1