重载遇到的问题
complex.h的
complex.cpp
main.cpp
不用看完整的类定义,只需要看下重载的+那个函数定义即可,我在main中只用了+做试验,但是用了之后,发现在gcc(code::blocks)下输出这种结果
我明白最后出现Construct Function!是用了RVO(Return Value Optimization),但是在调用重载+函数之前为何会有一个Copy Construct Function!
这是什么原因呢?它拷贝构造的谁呢?
还有一个问题,那就是拷贝构造函数如果在参数中不加const,运行结果会不对,原因是重载了double,但是我在拷贝构造函数里面也没有改变参数的值,为何会有区别呢?
以上两个问题,已困扰多日,希大侠帮忙解决下,谢谢了。
#if ! defined(COMPLEX_H_)
#define COMPLEX_H_
class complex
{
///Attributes:
private:
double real;
double image;
double radius;
double angle;
///Operations:
public:
complex(double real = 0, double image = 0);
complex(const complex &c);
~complex(){};
void setvalue(double real, double image);
double getreal(){return real;}
double getimage(){return image;}
double getradius(){return radius;}
double getangle(){return angle;}
complex operator +(complex comp);
complex operator *(complex comp);
complex operator !();
operator double();
};
#endif
complex.cpp
#include "complex.h"
#include <iostream>
#include <cmath>
using std::cout;
using std::endl;
complex::complex(double real, double image)
{
cout << "Construct Function!" << endl;
setvalue(real, image);
}
complex::complex(const complex &c)
{
cout << "Copy Construct Function!" << endl;
setvalue(c.real, c.image);
}
void complex::setvalue(double real, double image)
{
this->real = real;
this->image = image;
this->radius = sqrt(real * real + image * image);
this->angle = atan2(image, real);
}
complex complex::operator +(complex comp)
{
cout << "+ override function" << endl;
return complex(this->real + comp.real, this->image + comp.image);
}
complex complex::operator *(complex comp)
{
///(a+bi)(c+di) = ac + (ad+bc)i - bd = ac - bd + (ad+bc)i
return complex(this->real * comp.real - this->image * comp.image,
this->real * comp.image + this->image * comp.real);
}
///重载!求复数的共轭复数
complex complex::operator !()
{
return complex(this->real, -this->image);
}
///重载double()求复数的模
complex::operator double()
{
return fabs(radius);
}
main.cpp
#include <iostream>
#include "complex.h"
using std::cin;
using std::cout;
using std::endl;
int main(void )
{
double real1,image1,real2,image2;
cout << "Please the first complex:" << endl;
cin >> real1 >> image1;
cout << "Please the second complex:" << endl;
cin >> real2 >> image2;
complex comp1(real1, image1);
complex comp2(real2, image2);
cout << (comp1+comp2).getreal() << endl;
return 0;
}
不用看完整的类定义,只需要看下重载的+那个函数定义即可,我在main中只用了+做试验,但是用了之后,发现在gcc(code::blocks)下输出这种结果
Please the first complex:
1.1 2.2
Please the second complex:
3.3 4.4
Construct Function!
Construct Function!
Copy Construct Function!
+ override function
Construct Function!
4.4我明白最后出现Construct Function!是用了RVO(Return Value Optimization),但是在调用重载+函数之前为何会有一个Copy Construct Function!
这是什么原因呢?它拷贝构造的谁呢?
还有一个问题,那就是拷贝构造函数如果在参数中不加const,运行结果会不对,原因是重载了double,但是我在拷贝构造函数里面也没有改变参数的值,为何会有区别呢?
以上两个问题,已困扰多日,希大侠帮忙解决下,谢谢了。
...全文
相关推荐
重载函数实现变量排序问题
本文将详细介绍如何通过函数重载来解决变量排序的问题,并提供具体的代码示例。#### 一、问题描述本任务要求实现一个程序,该程序能够对两个整数和三个浮点数进行排序,并按照从小到大的顺序输出排序结果。
关于PHP属性重载问题
关于PHP的属性重载的问题,进行测试研究,证明private不继承下来。
C++运算符重载教学和编程中易被忽视的问题
当一个派生类继承了基类之后,如果基类中有运算符重载,那么派生类也可以重载相同的运算符,这涉及到运算符重载的继承性问题。
Vscode Python重载问题解决[源码]
文章提供了具体的解决方法:用户只需点击一个按钮切换到预发布版本的VS Code,即可解决持续出现的重载提示问题。这种方法非常直接有效,不需要进行复杂的设置或者深入的技术调试。
C++ new、delete(new[]、delete[])操作符重载需要注意的问题
当需要为类或对象分配内存时,程序员可以重载这些操作符以自定义内存管理的行为。然而,重载`new`、`delete`操作符需要注意一些关键问题,以确保正确性和兼容性。1.


