代码如下,具体情况是,我定义了一个有形参的构造函数和+号运算符重载,我觉得两个是独立的,但是在调用+号运算符重载时发现同时又调用了这个构造函数,因为我在函数里写了输出语句,所以被调用时我能看到,c++小白想请问各位大神是为什么?是运算符重载函数写的有问题吗?多谢各位~~
//constructeur 2
BigInt::BigInt(int n):numDigits(0)
{
cout << "constructeur 2 bien appelé" << endl;
int quotient = n;
int residu;
cout << "l'entier que vous donnez est ";
for(int i=0;i
{
numDigits++;
residu = quotient%10;
quotient = quotient/10;
vals[i] = residu;
if(residu == 0 && quotient == 0)
{
break;
}
cout
}
cout
cout
}
//surcharge de l'operateur+
BigInt BigInt::operator+(const BigInt& a)
{
cout
int temp[3000];
int r = 0;
for(int i=0;i
{
temp[i] = vals[i] + a.vals[i] + r;
if(temp[i]>9)
{
r = 1;
temp[i] -= 10;
}
else{r = 0;}
cout << temp[i] << " ";
}
cout << endl;
return temp[numDigits];
}
一个关于c++构造函数和运算符重载的问题
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答
- threenewbee 2016-02-08 23:15关注
你的判断是对的,重载的运算符会调用拷贝构造函数,把结果放在堆栈上,看下面的最简单的程序
#include <iostream> using namespace std; class A { public: A() { cout << "A()" << endl; } A(A& o){ cout << "A(A& o)" << endl; } A operator+(A& o){ return o; } }; int main() { A a, b; a + b; return 0; }
A()
A()
A(A& o)
Press any key to continue本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 python天天向上类似问题,但没有清零
- ¥30 3天&7天&&15天&销量如何统计同一行
- ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
- ¥15 C#调用python代码(python带有库)
- ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
- ¥15 活动选择题。最多可以参加几个项目?
- ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
- ¥15 vs2019中数据导出问题
- ¥20 云服务Linux系统TCP-MSS值修改?
- ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)