以下是否安全?嚴格地說,我知道,在它指向的東西被正確構造之前解引用指針看起來很危險,但我想象編譯器只是提供一個指針而不實際進行任何解引用。好吧,我想。C++在自己的施工過程中以*爲參考
(注意:gInst實際上不使用參考直到以後)
TU 1
Sys::System::System::System(const sf::VideoMode& rVM, const std::string& rSTR, unsigned long settings) :
win(rVM, rSTR, settings),
gInst(*this)
{
}
首部A
namespace Sys
{
namespace System
{
struct System;
}
namespace UI
{
struct GUI
{
System::System& topl;
MenuBar menu;
GUI(System::System&);
private:
GUI();
GUI(const GUI&);
GUI& operator=(const GUI&);
};
}
}
部首乙
namespace Sys
{
namespace System
{
struct System
{
sf::RenderWindow win;
Sys::UI::GUI gInst;
Sys::Editor::Editor eInst;
System(const sf::VideoMode&, const std::string&, unsigned long);
private:
System();
System(const System&);
System& operator=(const System&);
};
void start();
}
}
它取決於'gInst'函數,你沒有在這裏顯示。如果它只存儲指針/引用,並且在ctor拋出時不使用它,那麼你很好。 –
如果ctor拋出,然後gInst的析構函數被調用,對吧?暗示'gInst'的析構函數最好不要認爲它傳遞的'System'對象是好的。 –