C# Operator= overloading

   用C++写了类,来替代一些基本的类型,并保存更多的信息,提供给C#使用。

   ref class Test{
         
int i;
            int y;

   
public:
  Test(
int a){
         i 
= a;
      }


    
operator Test ^ operator=int a){
         
this.i = a;
         
return this;
      }

   }


  在C#中引用了一下:
Test  obj = new Test(123);
obj 
= 456;


   编译,C#编译发送错误,异常退出。看了手册,才知道 C#  不支持Operator= overloading。在C#中,要定义转换
public static implicit operator Test ( int i )
{
    
return new Test(i);
}

    但是如果这样转换,无法保证其中的成员 y 被复制。访问成员,非的要使用一个成员相关的名称变量。如果要 完全符合要求的替换 int ,是无法实现的。


   在http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=90874,微软给出了一些回复。好像也没有什么比较好的办法。
   http://blogs.extremeoptimization.com/jeffrey/archive/2005/02/19/153.aspx 这个是提问的人写的文章,
原文地址:https://www.cnblogs.com/thh/p/672149.html