关于C++/CLR中ref class类++重载的疑问。

pulllantern 2011-11-23 10:02:58
简单写了代码如下,也是看书中发现的问题。
不明白这个++的重载为何能起作用,重载函数中没有修改实参中的value值,而是修改了临时对象的value值,返回的也只不过是一个临时的句柄……
不明白,希望高人解答困惑。

#include "stdafx.h"
#include <iostream>
using namespace System;

ref class test
{
public:
int value;
public:
test(int i):value(i){}
static test^ operator ++(test^ test_ins);
virtual String^ ToString() override {return "The value of test_ins is: "+value;}
};

test^ test::operator ++ (test^ test_ins)
{
test^ tmp=gcnew test(test_ins->value);
++tmp->value;
return tmp;
}

int main(array<System::String ^> ^args)
{
test^ test_ins=gcnew test(0);
cout<<test_ins->value<<endl;
test_ins++;
Console::WriteLine(test_ins++);
Console::WriteLine(test_ins);
Console::ReadKey();
return 0;
}
...全文
288 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
程序员小迷 2012-02-05
  • 打赏
  • 举报
回复
非CLR的C++不是也返回一个变量吗?这没什么啊,外部使用会直接赋值的
pulllantern 2011-11-23
  • 打赏
  • 举报
回复
求高人指点啊。
taodm 2011-11-23
  • 打赏
  • 举报
回复
clr问题请自己移贴到C#版。

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧