c++小技巧_静态方法
在看虚幻源码的时候发现的这个技巧,以前没用过。
利用静态变量只会初始化一次,再利用构造函数就可以在里面做逻辑,逻辑结果保存在一个成员中,下次再调这个方法则不会再次初始化(也就是不会再次调构造逻辑)
- 源码在 AsyncLoading.cpp 中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27/** True if multithreaded async loading should be used. */
static FORCEINLINE bool IsMultithreaded()
{
static struct FAsyncLoadingThreadEnabled
{
bool Value;
FORCENOINLINE FAsyncLoadingThreadEnabled()
{
if (FPlatformProperties::RequiresCookedData())
{
check(GConfig);
bool bConfigValue = true;
GConfig->GetBool(TEXT("/Script/Engine.StreamingSettings"), TEXT("s.AsyncLoadingThreadEnabled"), bConfigValue, GEngineIni);
bool bCommandLineNoAsyncThread = FParse::Param(FCommandLine::Get(), TEXT("NoAsyncLoadingThread"));
bool bCommandLineAsyncThread = FParse::Param(FCommandLine::Get(), TEXT("AsyncLoadingThread"));
Value = bCommandLineAsyncThread || (bConfigValue && FApp::ShouldUseThreadingForPerformance() && !bCommandLineNoAsyncThread);
}
else
{
Value = false;
}
}
} AsyncLoadingThreadEnabled;
return AsyncLoadingThreadEnabled.Value;
}