首页新闻找找看学习计划

C#中实现C++中的引用指针的问题

0
悬赏园豆:5 [已关闭问题]
<P>先看段可行的代码:</P> <P>using System;</P> <P>class Singleton<BR>{<BR>&nbsp;private int use = 0;<BR>&nbsp;private int x; <BR>&nbsp;private int y;<BR>&nbsp;private static Singleton []instance;<BR>&nbsp;private static object lockHelp = new Object();<BR>&nbsp;private static int size;<BR>&nbsp;private Singleton()<BR>&nbsp;{}<BR>&nbsp;public static void Init(int x)<BR>&nbsp;{<BR>&nbsp;&nbsp;size = x;<BR>&nbsp;&nbsp;instance = new Singleton[size];<BR>&nbsp;}</P> <P>&nbsp;public static Singleton getInstance()<BR>&nbsp;{<BR>&nbsp;&nbsp;if(instance == null)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;lock(lockHelp)<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;if(instance == null)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance = new Singleton[10];<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;for(int i = 0; i &lt; size; i++)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;lock(lockHelp)<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;if(instance[i] == null)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance[i] = new Singleton();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance[i].use = 1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return instance[i];<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;else<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(instance[i].use == 0)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance[i].use = 1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return instance[i];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;return null;<BR>&nbsp;}<BR>&nbsp;public int X<BR>&nbsp;{<BR>&nbsp;&nbsp;get<BR>&nbsp;&nbsp;{&nbsp;return x;&nbsp;}<BR>&nbsp;&nbsp;set<BR>&nbsp;&nbsp;{&nbsp;x = value;&nbsp;}<BR>&nbsp;}<BR>&nbsp;public int Y<BR>&nbsp;{<BR>&nbsp;&nbsp;get<BR>&nbsp;&nbsp;{&nbsp;return y;&nbsp;}<BR>&nbsp;&nbsp;set<BR>&nbsp;&nbsp;{ &nbsp;y = value;&nbsp;}<BR>&nbsp;}<BR>&nbsp;public void dayin()<BR>&nbsp;{<BR>&nbsp;&nbsp;Console.WriteLine("这个实例的X为{0},Y为{1},使用计数为{2}", x, y, use);<BR>&nbsp;&nbsp;<BR>&nbsp;}<BR>&nbsp;public static void del(Singleton s)<BR>&nbsp;{<BR>&nbsp;&nbsp;for(int i = 0; i &lt; size; i++)<BR>&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;if(instance[i] == s)<BR>&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;s = instance[i] = null;<BR>&nbsp;&nbsp;&nbsp;&nbsp;break;<BR>&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;}<BR>&nbsp;}<BR>}</P> <P><BR>class Test<BR>{<BR>&nbsp;public static void Main()<BR>&nbsp;{<BR>&nbsp;&nbsp;Singleton.Init(10);<BR>&nbsp;&nbsp;Sin
问题补充: class Test { public static void Main() { Singleton.Init(10); Singleton s1 = Singleton.getInstance(); s1.X = 3; s1.dayin(); Singleton s2 = Singleton.getInstance(); s2.dayin(); Singleton s3 = Singleton.getInstance(); s3.dayin(); Singleton s4 = Singleton.getInstance(); s4.dayin(); Singleton s5 = Singleton.getInstance(); s5.dayin(); Singleton s6 = Singleton.getInstance(); s6.dayin(); Singleton s7 = Singleton.getInstance(); s7.dayin(); Singleton s8 = Singleton.getInstance(); s8.dayin(); Singleton s9 = Singleton.getInstance(); s9.dayin(); Singleton s10 = Singleton.getInstance(); s10.dayin(); Singleton.del(s4); Singleton s11 = Singleton.getInstance(); s11.dayin(); Console.ReadLine(); } } 这样删除一个对象池中的对象有点麻烦,我希望直接设置 s1 = null;这一句话就OK 所以将函数改为:public static void getInstance(ref Singleton s) 外面: public static void Main() { Singleton.Init(10); Singleton s = null; Singleton.getInstance(ref s); s.dayin(); } 但出现未将对象的引用设置到对象的实例的错误 修改后的方法: public static void getInstance(ref Singleton s) { if(instance == null) { lock(lockHelp) { if(instance == null) { instance = new Singleton[10]; } } } for(int i = 0; i < size; i++) { lock(lockHelp) { if(instance[i] == null) { instance[i] = new Singleton(); instance[i].use = 1; s = instance[i]; } else { if(instance[i].use == 0) { instance[i].use = 1; s = instance[i]; } } } } s = null; }
irons的主页 irons | 初学一级 | 园豆:11
提问于:2007-12-21 08:28
< >
分享
其他回答(2)
0
更改后的代码是什么样的,在哪里出错的?
Anders Cui | 园豆:1570 (小虾三级) | 2007-12-21 10:30
0
ref使用前,变量必须赋值。 Singleton s = null;空引用相当于没有赋值。 Singleton.getInstance(ref s); 不明白你的意图。
暗香浮动 | 园豆:361 (菜鸟二级) | 2007-12-21 10:37
0
即然是Singleton,就没必要释放内存。一个对象再大能占用多少内存?所谓内存泄漏一般是指连续分配内存又不释放,导致随着程序使用的时间越来越长系统内存越来越少最后崩溃的情况。单件不会造成这种情况。
1-2-3 | 园豆:200 (初学一级) | 2007-12-24 10:25
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册