一个类中含有const数据成员,这个类的2个对象之间如何才能用=号赋值?
时间:2011-12-13
来源:互联网
C/C++ code
编译结果:
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o Practise_5_5_1.o ..\Practise_5_5_1.cpp
..\Practise_5_5_1.cpp: In member function 'complex& complex::operator=(const complex&)':
..\Practise_5_5_1.cpp:13: error: non-static const member 'const double complex::consum', can't use default assignment operator
..\Practise_5_5_1.cpp: In function 'int main()':
..\Practise_5_5_1.cpp:84: note: synthesized method 'complex& complex::operator=(const complex&)' first required here
#include <iostream> using namespace std; class complex { private: //定义实数部分和虚拟部分 double real,image; //5_5_2题 定义一个常成员 const double consum; public: //complex(); complex(double, double); ~complex(); //复制构造函数 complex( const complex &); double GetReal(); double GetImage(); void Show(); //声明友元函数add friend complex add(complex, complex); //5_5_2题 声明一个常成员函数;const关键字必须放在函数参数表之后 void ConShow ( ) const ; }; //complex::complex() //{ //} complex::complex( const complex &ccp):consum(0) //有没有这个复制构造函数,都报error: non-static const member 'const double complex::consum', can't use default assignment operator { real = ccp.real; image = ccp.image; } complex::complex(double x, double y):consum(x) { real = x; image = y; } complex::~complex() { } double complex::GetReal() { return real; } double complex::GetImage() { return image; } //5_5_2题 定义一个常成员函数,Page114上说定义const成员函数时,const关键字可以放在参数列表前,也可以放在参数列表后 void complex::ConShow () const { cout << consum << endl; } complex add(complex a, complex b) { complex sum(0,0); sum.real = a.real + b.real; sum.image = a.image + b.image; return sum; } int main( ) { complex one(3.3,5.6), two(1.5,-0.2), three(0,0); three = add(one, two); //该如何写才能让这句话可以正确执行呢? //three(add(one, two)); cout << "a + b 的实数部分 = " << three.GetReal() << endl; cout << "a + b 的虚数部分 = " << three.GetImage() << endl; return 0; } //运行结果:
编译结果:
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o Practise_5_5_1.o ..\Practise_5_5_1.cpp
..\Practise_5_5_1.cpp: In member function 'complex& complex::operator=(const complex&)':
..\Practise_5_5_1.cpp:13: error: non-static const member 'const double complex::consum', can't use default assignment operator
..\Practise_5_5_1.cpp: In function 'int main()':
..\Practise_5_5_1.cpp:84: note: synthesized method 'complex& complex::operator=(const complex&)' first required here
作者: loveandy183 发布时间: 2011-12-13
cast_const
作者: qscool1987 发布时间: 2011-12-13
你需要自己定义complex类的赋值运算符,原型如下:
complex& complex::operator=(const complex&);
一般编译器会自动为你生成这个函数,但是当类中有const成员或引用成员时,它就不会生成了,因为它不知道该如何做。你必须自己定义这个函数,在其中忽略该const成员的赋值。
complex& complex::operator=(const complex&);
一般编译器会自动为你生成这个函数,但是当类中有const成员或引用成员时,它就不会生成了,因为它不知道该如何做。你必须自己定义这个函数,在其中忽略该const成员的赋值。
作者: tonywearme 发布时间: 2011-12-13
C/C++ code
class complex { // ... complex& operator=(complex& c) { real=c.real; image=c.image; double *temp=const_cast<double*>(&consum); *temp=c.consum; return *this; } };
作者: yisikaipu 发布时间: 2011-12-13
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28