「指針」保存「Int」的地址。我想通過參照地址傳遞給我的給類:將地址傳遞給一個類,並從那裏通過C++中的引用向其「子級」
class N {
public:
N(int &pPointer){
std::cout << "Address: " << &(pPointer) <<" \n";
}
};
class M {
public:
M(int &pPointer):n(pPointer) {
std::cout << "Address: " << &pPointer <<" \n";
}
private:
N n;
};
int main() {
int Int = 5;
int *pointer = ∬
std::cout << "Address: " << pointer <<" \n";
M m(*pointer);
return 0;
}
這是一個很好的做法(因爲我是一種使用參照復引用指針)? 還是絕對可怕? 我只是想在這裏避免指針。 (儘管我在開始時被迫使用「*指針」)。
爲什麼你不得不在開始時使用'*指針'?爲什麼不使用'M m(Int);'? –
好問題!在我的項目中,我想將「全局」屏幕傳遞給一些渲染函數。屏幕必須是指向SDL_Surface的指針,因爲初始函數SDL_SetVideoMode返回指向SDL_Surface的指針。看到這裏:http://www.libsdl.org/docs/html/sdlsetvideomode.html – user1511417