简单问题,快来抢分:哪些操作符不可以作为类的成员来重载(只能用friend来重载)? 重载操作符时,有的操作符(如 <<, >>)用friend来重载可以, 可不可以作为类的成员来重载。或者说哪些操作符不可以作为类的成员来重载(只能用friend来重载)。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我感觉除<< , >>大部分类的成员来重载如: +_*/ == != . :: * -> ? :好象都不能重载,不论用friend还是作为类的成员!剩下的好象都可以作为friend或类的成员被重载! 如 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;} * 和 -> 是可以重载的,如下:CMemDC * operator->(){return this;}operator CMemDC*(){return this;}“将这两个算符(<<, >>)用类成员来重载”当然是可以的,但就不是做为插入/析取符了。重载运算符为类的成员,类必须在算符的左边。 friend ostream& operator << (ostream&, const CMatrix&); friend istream& operator >> (istream&, CMatrix&);声明为friend是因为该重载操作符后的函数要被其他类的对象调用,例子中就是被ostream的对象调用 http://202.113.96.10/ini/c++/flag_chong.htm 不知道为什么有这样的要求。作为类成员是可以的。不过看上去可能有点奇怪。第一种方法:重载为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))));重载为友员是最好的。后面哪位大侠有更好的方法,请贴上。 如果operator <<不用直接使用CMatrix的私有成员变量的话,不申明为友元也是可以的。 to:yndfcd(YNDFCD)我觉得在你的第二段代码中,用>>更直观一些。即改为:ostream & operator >> (ostream & os)及:m1>>(m2>>(m3>>(m4>>cout)));不过这样输出的是m4 m3 m2 m1 最近看到做界面时使用HTML,用CDHtmlDialog究竟效率如何,大家讨论一下 VC关于操作方面的问题,求指教 如何判断用户已经正常登陆Windows? 问一个及其简单的类型转换问题 谁能提供《算法设计与分析》习题集与解答? 那儿有VC下的? 如何将位图拉伸显示,急 dll中如何使用mpi并行计算? 如何调用? Dll参数为引用有不好吗? 我想用MFC制作一个圆角的按钮,该如何做。本人刚学MFC,求大神们给点详细的 如何读取Combobox中写入的数据 关于ISAPI的问题,请高手指点
大部分类的成员来重载
如: +_*/ == !=
剩下的好象都可以作为friend或类的成员被重载!
哪位高手帮忙将 这两个算符(<<, >>)用类成员来重载(该程序运行无误),谢谢。// 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;
}
CMemDC * operator->(){return this;}
operator CMemDC*(){return this;}“将这两个算符(<<, >>)用类成员来重载”当然是可以的,但就不是做为插入/析取符了。
重载运算符为类的成员,类必须在算符的左边。
friend istream& operator >> (istream&, CMatrix&);声明为friend是因为该重载操作符后的函数要被其他类的对象调用,例子中就是被ostream的对象调用
重载为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))));重载为友员是最好的。后面哪位大侠有更好的方法,请贴上。
我觉得在你的第二段代码中,用>>更直观一些。即改为:
ostream & operator >> (ostream & os)
及:
m1>>(m2>>(m3>>(m4>>cout)));
不过这样输出的是m4 m3 m2 m1