关于C++/CLR中ref class类++重载的疑问。
简单写了代码如下,也是看书中发现的问题。
不明白这个++的重载为何能起作用,重载函数中没有修改实参中的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;
}