anhaoxuan 2016-04-30 09:28 采纳率: 100%
浏览 2029
已采纳

关于C++加法运算符重载的一个超简单问题

以下代码:
#include
using namespace std;
class num
{
public:
num(){n=1;cout<<"构造函数执行\n";}
num(int i){n=i;cout<<"带一个参数的构造函数执行\n";}
num(const num&s){this->n=s.n;cout<<"复制构造函数执行\n";}
~num(){cout<<"析构函数执行\n";}
int get()const{return n;}
void set(int x){n=x;}
const num operator+(const num&r){return num(n+r.get());}

private:
int n;
};
int main()
{
num one(1),two(2),three;
three=one+two;
cout<<"one:"<<one.get()<<endl;
cout<<"two:"<<two.get()<<endl;
cout<<"three:"<<three.get<<endl;
return 0;

}

运行后three的结果我希望是3
但程序返回的是1
请问问题出在哪里?

  • 写回答

3条回答 默认 最新

  • futl 2016-04-30 12:05
    关注

    事实证明结果的确就是3啊图片说明

    还有,你的代码里three.get都没有“()”,真的能运行通不报错吗?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler