找回密码
 立即注册

QQ登录

只需一步,快速开始

tbugs
中级会员   /  发表于:2011-3-30 23:56  /   查看:9399  /  回复:17
求回复。
路,在此绽放。

17 个回复

倒序浏览
robert
金牌服务用户   /  发表于:2011-3-31 09:20:00
沙发
看不懂,你说的是什么结构?
只要是.NET支持的功能都可以用。
回复 使用道具 举报
tbugs
中级会员   /  发表于:2011-3-31 09:46:00
板凳
class Node
{
    ....
    public Node parent;//该节点父亲
    ....
}

这个是可以用class表达的,但是不能用struct进行这样的表达该parent。
然而,我非要用struct表达,才能实现该节点数组的内存拷贝(使用Array.Copy),不然拷的都是引用...
请问有解决方法吗?
昨晚和小胖讨论了N久,都没结果...最后让小胖睡了。
路,在此绽放。
回复 使用道具 举报
quab1
高级会员   /  发表于:2011-3-31 09:46:00
地板
感觉没什么结构表示不出来的 毕竟C#的引用可以为null
特殊点的
有 ref 从外面传入
有 out 从内部传出

跟C++描述接口的 [in] [out] 用法挺像的
垃圾代码的创造者。。。
回复 使用道具 举报
quab1
高级会员   /  发表于:2011-3-31 09:55:00
5#
class Node
{
    ....
    public Node parent;//该节点父亲
    ....
    Node( Node other)
    {
        this.xx = other.xx;
        ....
    }
}

拷贝的时候

Node a = new Node( other );
垃圾代码的创造者。。。
回复 使用道具 举报
tbugs
中级会员   /  发表于:2011-3-31 12:50:00
6#
不是拷贝这个Node类,是拷贝由这个Node类的二维数组。

例如 Node[20,20] 这样的二维数组。
路,在此绽放。
回复 使用道具 举报
Arthas
葡萄城公司职员   /  发表于:2011-3-31 15:00:00
7#

回复 2# robert 的帖子

lz意思是编译的时候放开unsafe关键字。
扯淡第一高手
回复 使用道具 举报
Arthas
葡萄城公司职员   /  发表于:2011-3-31 15:10:00
8#
tbugs的要求其实很简单
struct呢, 会实现完全的数据copy, 而不是拷引用。
我们知道class实际上拷的是引用。

但是struct不允许在里面用自己做parent。
class可以, 但是又不好控制Array.Copy时候的拷贝。
因此, 他,
大概就是要开放这样的写法:

  1. namespace ConsoleApplication12
  2. {
  3.     unsafe class Program
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             Point parent = new Point()
  8.             {
  9.                 Width = 5,
  10.                 Height = 3
  11.             };
  12.             Point child = new Point()
  13.             {
  14.                 Width = 7,
  15.                 Height = 8,
  16.                 parent =  &parent
  17.             };

  18.             Console.WriteLine(child.parent->Width);
  19.             Console.WriteLine(child.parent->Height);
  20.         }
  21.     }

  22.     public unsafe struct Point
  23.     {
  24.         public int Width;
  25.         public int Height;

  26.         public Point* parent;
  27.     }
复制代码
扯淡第一高手
回复 使用道具 举报
Arthas
葡萄城公司职员   /  发表于:2011-3-31 15:14:00
9#
关键问题在于Point是struct的。
扯淡第一高手
回复 使用道具 举报
robert
金牌服务用户   /  发表于:2011-3-31 15:17:00
10#

搞这么复杂就为了调用ArrayCopy,自己遍历下原来的Array创建个新的不就行了...
回复 使用道具 举报
12下一页
您需要登录后才可以回帖 登录 | 立即注册
返回顶部