我有节点结构(它在下一个相同的结构上包含值)。

struct Node {
    Node *nextElem;
    Element thisValue;
};


我想在填充它的函数中传递空(null)node.ByReference。

// C++
Element element = ...; //fills in another function;
Node    *list   = NULL;
AddElementToList(element, &list);
// which declered as
void AddElementToList (Element element, Node * list) {...}

// Java
Element.ByValue  element = ...; //fills great in another function in the same way ByReference (and reconstructed as ByValue),
                                //but initialize with trash in Pointers and without recurtion;
Node.ByReference list    = null;
MyDll.INSTANCE.AddElementToList(element, list);


所以如果我用

Node.ByReference list = null;


当C ++端尝试读取列表时,像任何空指针一样,我收到无效的内存访问错误。
所以我正在尝试初始化列表。但是在那种情况下,我必须初始化下一个节点,下一个节点和...

最佳答案

我通过在PointerByReference中包装Node来找到解决方案:

// method declaration:
void AddElementToList(Element element, PointerByReference wrapedNodeInPointerByRef);


用法:

Element.ByValue element = ...;
PointerByReference list = new PointerByReference();
MyDll.INSTANCE.AddElementToList(element, list); // yes, Element.ByValue puts in Element

// but to get **Node** from filled PointerByReference you should reparse it like:
Node node = new Node(list.getValue());


对于该创建构造函数:

public Node (Pointer value) {
 super(value);
 read();
}


我已经以相同的方式获得Node.ByValue和Node.ByReference的构造函数。
该示例是复杂程序的简化版本,具有更多抽象,但希望不要丢失,对某些人有所帮助。

一些想法:


如果PointerByReference可以为空,则Structure.ByReference的不能为空吗?
尚不清楚为什么Element.ByValue像Element那样工作,但是当用Element.ByValue声明时,会导致无效的内存访问。

10-08 13:06