Tru*_*eon 2 c++ constructor reference this
以下是安全的吗?我知道,严格来说,在指向它的东西之前解除引用指针似乎很危险,但我想编译器只提供一个指针而不实际进行任何解除引用.好吧,我想.
(注意:gInst实际上直到很久才会使用该引用.)
TU 1
Sys::System::System::System(const sf::VideoMode& rVM, const std::string& rSTR, unsigned long settings) :
win(rVM, rSTR, settings),
gInst(*this)
{
}
Run Code Online (Sandbox Code Playgroud)
标题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&);
};
}
}
Run Code Online (Sandbox Code Playgroud)
标题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();
}
}
Run Code Online (Sandbox Code Playgroud)
(注意:gInst实际上直到很久才会使用该引用.)
然后它是完全安全的.(注意你说"参考",而不是"价值".)
由于你所说的原因,编译器会对此发出警告,但没有什么不确定的,只是"冒险".请注意,您可以经常使用以下内容胜过编译器警告(如果他们打扰您):
struct foo
{
foo() : b(self())
{}
private:
foo& self() { return *this; }
bar b;
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10835 次 |
| 最近记录: |