首页 新闻 会员 周边

重载隐式类型转换运算符时候的问题

0
悬赏园豆:20 [已关闭问题] 关闭于 2008-08-01 16:00
我想重载一个类的隐式转换运算符<br><div class="cnblogs_code"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><img src="/Images/OutliningIndicators/None.gif" align="top"><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">implicit</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">operator</span><span style="color: rgb(0, 0, 0);">&nbsp;MyClass</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">T</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">(T&nbsp;value)</span></div>这是格式<br>但是每次进行类型转换,都会创建新的MyClass对象,这可不是我想要的,而C#中又不允许重载赋值运算符<br>请问各位博友,有没办法在执行一个<br><div class="cnblogs_code"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><img src="/Images/OutliningIndicators/None.gif" align="top"><span style="color: rgb(0, 0, 0);">MyClass</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;str&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;MyClass</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">();<br><img src="/Images/OutliningIndicators/None.gif" align="top">str&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">Hello&nbsp;World!</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;</span></div>的时候,str<b>不会</b>被重新创建呢?根据str.GetHashCode()来看,现在是会被创建新的对象。<br>
问题补充: MyClass<string> str = new MyClass<string>(); str = "Hello World!"; 这样赋值是为了让它“看起来”是可以直接赋值的,但是我只能想到通过重载隐式类型转换运算符的方法来实现,这会产生新对象。 不过我又突然有了新的想法,就是重写GetHashCode和Equal方法,让他“看起来”像是同一个对象的引用。 不知道这种方法可不可以了。
JimLiu的主页 JimLiu | 菜鸟二级 | 园豆:300
提问于:2008-07-27 11:08
< >
分享
所有回答(2)
0
如果要用"="来赋值的话,在目前的C#里是没有办法的. 可以用Method,比如 public void LoadValue(T value) { ... } 然后 MyClass<string> str = new MyClass<string>(); str.LoadValue("hello world"); 或者使用constructor, property也行.
deerchao | 园豆:8367 (大侠五级) | 2008-07-27 20:28
0
可以尝试一下用单件模式。 把MyClass的构造函数,变为私有。并增加静态成员私有变量 : static private MyClass_myclass; 创建下面的函数: static public MyClass GetInstance() { if(_myclass==null) { _myclass=new MyClass(); } return _myclass; } 调用 时,只要用MyClass.GetInstance();得到MyClass对象。
hackenliu | 园豆:600 (小虾三级) | 2008-07-28 17:21
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册