所以我想實現在C廠鏈表的運行++爲什麼解引用節點會破壞我的鏈表?
template<class T>
class Node
{
private:
Node *next;
T item;
public:
Node(T item)
: item(item)
{
this->next = NULL;
}
Node<T> add(T item) {
this->next = new Node(item);
return *this->next;
}
bool hasNext()
{
return this->next == NULL;
}
Node<T> getNext()
{
return *this->next;
}
T value()
{
return this->item;
}
};
void main()
{
Node<int> node(3);
node.add(3).add(4);
cout << node.value();
cout << node.getNext().value();
cout << node.getNext().getNext().value();
cin.get();
}
,但我無法得到它的工作。尤其是本節:
node.add(3).add(4);
cout << node.value();
cout << node.getNext().value();
cout << node.getNext().getNext().value();
如果我改變add
和getNext
函數返回Node<T>*
,而不是Node<T>
,它工作正常。但爲什麼解引用會導致代碼崩潰?我認爲.
表示法比->
更有意義,但我無法使其工作。我究竟做錯了什麼?
被製成如果你有'add'返回一個節點'',則返回一個_copy_您正在然後修改。這意味着你會掉到列表的尾部:-)你正在尋找一個參考 - 在這兩個函數上返回'Node &'。 –
2013-05-09 15:53:59
垃圾工程,真棒。那麼C++中的函數總是按值傳遞呢? (事先道歉我的c#/ java背景) – sircodesalot 2013-05-09 15:56:19
是的。參考文獻通過,以及參考。它們的一個常用用途恰恰就是你的用例,它們通常也被用作'const T&',這樣你就可以通過引用,但確保沒有任何修改值。 – 2013-05-09 15:58:14