我有點困惑,當涉及到指針和c + +中的引用。返回對象從一個函數,如鏈接列表中的指針
我知道只有指針可以爲null。因此,對於我的自定義鏈表類,我有:
ObjectNode* head; // Pointer to the first item of the list
ObjectNode* last; // Pointer to the last item of the list
同樣的,在我的ObjectNode課,我有
SimObject object; // The actual Object (cannot be null)
ObjectNode* next; // Pointer to next Node
ObjectNode* prev; // Pointer to previous Node
這樣一來,我的未來和prev變量可以爲空,使用
next = null;
但是,我get方法:
ObjectNode ObjectList::getHead() {
return head;
}
ObjectNode ObjectList::getLast() {
return last;
}
這給出了將發生的轉換錯誤。所以我可能誤解了一些重要的東西,我不確定我是如何返回該對象的?
啊對我看到 我什至沒有考慮使用具有給類以外的檢查空值會是次優 感謝您向我明確解釋 – Astantos