[已解决问题] C#中实现C++中的引用指针的问题
提问时间: 2007-12-21 08:28
悬赏分:5 浏览:561 次

先看段可行的代码:

using System;

class Singleton
{
 private int use = 0;
 private int x;
 private int y;
 private static Singleton []instance;
 private static object lockHelp = new Object();
 private static int size;
 private Singleton()
 {}
 public static void Init(int x)
 {
  size = x;
  instance = new Singleton[size];
 }

 public static Singleton getInstance()
 {
  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;
     return instance[i];
    }
    else
    {
     if(instance[i].use == 0)
     {
      instance[i].use = 1;
      return instance[i];
     }
    }
   }
  }
  return null;
 }
 public int X
 {
  get
  { return x; }
  set
  { x = value; }
 }
 public int Y
 {
  get
  { return y; }
  set
  {  y = value; }
 }
 public void dayin()
 {
  Console.WriteLine("这个实例的X为{0},Y为{1},使用计数为{2}", x, y, use);
  
 }
 public static void del(Singleton s)
 {
  for(int i = 0; i < size; i++)
  {
   if(instance[i] == s)
   {
    s = instance[i] = null;
    break;
   }
  }
 }
}


class Test
{
 public static void Main()
 {
  Singleton.Init(10);
  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;
}

所有回答(3)
更改后的代码是什么样的,在哪里出错的?
8个月前   回答者:Anders Cui - 小虾三级
ref使用前,变量必须赋值。
Singleton s = null;空引用相当于没有赋值。
Singleton.getInstance(ref s);

不明白你的意图。
8个月前   回答者:暗香浮动 - 菜鸟二级
即然是Singleton,就没必要释放内存。一个对象再大能占用多少内存?所谓内存泄漏一般是指连续分配内存又不释放,导致随着程序使用的时间越来越长系统内存越来越少最后崩溃的情况。单件不会造成这种情况。
8个月前   回答者:1-2-3 - 菜鸟二级
评论
   您需要登录以后才能回答!
我的问题    我要提问


快到期问题

> 问题排行榜

有不合适内容,建议去除