首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

请问一个代码复用的有关问题

2012-04-07 
请教一个代码复用的问题有一系列的类对某些结构体进行操作,每个类中的结构体不同,但对结构体的操作都是类

请教一个代码复用的问题
有一系列的类对某些结构体进行操作,每个类中的结构体不同,但对结构体的操作都是类似的。想复用这些操作的代码,不知要如何进行。
如:
class   A{
      struct   S1{
            ...
      }s1;
      fun1(S1);
      fun2(S1);
}
class   B{
      struck   S2{
              ...
      }s2;
      fun1(S2);
      fun2(S2);
}

两个类中的fun1(),fun2()的代码是一样的。

谢谢大家!

[解决办法]
睡了一觉就想通了,再来试一下:
using System;
public abstract class Base
{
protected static ArrayList al = new ArrayList();
protected static ArrayList als = ArrayList.Synchronized(al);
protected abstract uint GetID(object it);
public bool del(object it)
{
lock (als.SyncRoot)
{
int num = als.Count;
for (int i = 0; i < num; i++)
{
if (GetID(als[i]) == GetID(it))
{
als.RemoveAt(i);
break;
}
}
if (num == als.Count)
{
return false;
}
return FileWrite();
}
}
}

public class MyClass : Base
{
public unsafe struct saveStruct
{
public fixed char Name[5];
public fixed char Ico[5];
public byte Type;
public uint ID;
};
protected override uint GetID(object it)
{
return ((saveStruct)it).ID;
}
}
C#语法不是很熟,不知道可不可以?

热点排行