我通常在C中工作,但現在我必須使用C++。我有一個大文件,有很多重複,因爲我目前可以不循環遍歷dir_x到dirx_z。
1.是否有辦法讓這個類中的元素可尋址,就好像它是一個數組?我在最後一行給出了一個例子。
2.我現在指的我可以在類聲明中聲明一個數組,該數組稍後將被該類的元素填充?
Node * dir_x;
爲紐帶,但什麼是真正的名字,所以我可以google一下?
class Node {
public:
Node(int x, int y, int z){
//Will be initialized to point to the next Node (neighbor)
//So will be holding elements of this same class!
dir_x = NULL;
dir_y = NULL;
dir_z = NULL;
}
//These are "links", but does anybody know the name to google it?
Node * dir_x;
Node * dir_x;
Node * dir_x;
};
//Code snippet of a function:
//current would be the node to traverse through the collection
Node * current = someNode;
//together with next
Node * next = NULL;
//Here the next node is actually set
next = current->dir_x;
//But I would like a generic way like below
//to reduce code duplication by about a third:
next = current->dir[i];
你可以讓dir成爲Node * dir [3]。但是,你不應該有公共會員敦敦杜恩。 – IdeaHat
這個詞是「指針」。 –
你爲什麼試圖聲明三個同名的成員?這永遠不會起作用。 –