将2D数组传递给dif。类 [英] Pass 2D array to dif. class

查看:61
本文介绍了将2D数组传递给dif。类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将二维数组传递给另一个类?

这是我目前所拥有的一点:

How can i pass a 2d array to another class?
This is a little of what i have at the moment:

展开 | 选择 | Wrap | 行号

推荐答案

您是否考虑过创建2D数组的类的范围?


如果实例是创建数组的类超出了范围,它的析构函数被调用。通常,当发生这种情况时,将删除/清除所有变量,包括您定义的2D阵列。这意味着2D数组也将从正在使用它的类中删除(因为在传递数组时,实际上只是将指针传递到内存位置)


当您检索时父/调用类中的2D数组会复制它,看看是否能解决问题。


-Frinny
Have you considered the scope of the class that the 2D array was created in?

If the instance of the class that creates the array goes out of scope it''s deconstructor is called. Typically when this happens all variables, including the 2D array you have defined, will be deleted/cleaned up. This would mean that the 2D array would also be deleted from the class that is using it (because when passing arrays you are actually just passing a pointer to a memory location)

When you retrieve the 2D array in the parent/calling class make a copy of it and see if that solves the problem.

-Frinny


你的方式现在有你的代码,NewConnection是一个私有嵌套类。那是你的意图吗?或者你只是从类ocl中省略了一个大括号?
The way you have your code now, NewConnection is a private nested class. Was that your intention? Or did you just leave out a brace from class ocl?


另外,你将一个套接字和一个数组传递给NewConnection构造函数,但是构造函数只接受一个参数......也许那就是你的错误指的是什么?

Frinavale :我可能错了,但我相信Framework GarbageCollector不会清理任何对象,如果仍有对它们的引用。所以在这种情况下,即使Ocl超出范围,NewConnection.blist(如果它是一个字段)仍然会保持对数组本身的有效引用。


如果Ocl去了超出范围,然后Ocl.blacklist(如果它是一个字段)将返回一个空引用异常。
Also, you are passing in a socket and an array to the NewConnection constructor, but there the constructor only accepts one parameter...perhaps that is what your error is referring to?

Frinavale: I may be wrong, but I believe that the Framework GarbageCollector will not clean up any objects if there is still a reference to them. So in this case, even if Ocl goes out of scope, the NewConnection.blist (if it were a field) would still hold a valid reference to the array itself.

If Ocl went out of scope, then Ocl.blacklist (if it were a field) would return a null reference exception.


这篇关于将2D数组传递给dif。类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆