Is the following safe? I know, strictly speaking, dereferencing a pointer before the thing to which it points has been properly constructed seems dangerous, but I imagine the compiler will just provide a pointer without actually doing any dereferencing. Well, I assume.
(Note: gInst doesn’t actually use the reference until much later.)
TU 1
Sys::System::System::System(const sf::VideoMode& rVM, const std::string& rSTR, unsigned long settings) :
win(rVM, rSTR, settings),
gInst(*this)
{
}
Header 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&);
};
}
}
Header B
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();
}
}
Then it’s completely safe. (Taking note you say “the reference”, not “the value”.)
Compilers will just warn about it for the reason you’ve stated, but there’s nothing undefined about it, just “risky”. Note that you can often trump compiler warnings (if they bother you) with something like this: