抱歉,你回答的那个问题,我选最佳答案的时候给错了。再问一个问题……const Complexdefds123是什么意思呢?

[复制链接]
查看11 | 回复1 | 2011-3-27 11:49:01 | 显示全部楼层 |阅读模式
#includeiostream
using namespace std;
class Complex {
public:
Complex( double r= 0.0, double m = 0.0 ):
real(r),imaginary(m){
}
// constructor
Complex operator ( const Complex// addition
Complex operator-( const Complex
//subtraction
const Complex
// assignment
void shownum(){
coutreal\" \"imaginaryendl;
}
private:
double real;
// real part
double imaginary;// imaginary part
};
const Complex

imaginary = right.imaginary;

return *this;
}
Complex Complex::operator (const Complex
}
// Overloaded subtraction operator
Complex Complex::operator- (const Complex
}

int main(){
Complex x, y( 4.3, 8.2 ), z( 3.3, 1.1 );

x = y z; //
x = y - z;
//
}
回复

使用道具 举报

千问 | 2011-3-27 11:49:01 | 显示全部楼层
<pre id=\"best-answer-content\" class=\"reply-text mb10\">Complexdefds123表示对类Complex对象的引用。在此引用参数前添加const,这参数也就成为常量参数。常量与变量相对,顾名思义就是值不会改变。

如果在引用对象过程中,不希望对该对象的内容进行修改时,可以用const来标记,使编译器知道参数不能被改变。

也就是说在你的参数中用了const,如果你再对Complex中的元素real,imagenary等重新赋值的话,编译器会给出错误信息。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行