Poo*_*ria 1 c++ constructor initialization reference dllexport
让我们假设我们有一个DLL,并且应该有一个全局存储在其中的数组将被导出,我们想要通过从文件中读取一些内容来初始化它,所以我个人发现自己别无他法它在一个结构中能够使用构造函数初始化:
struct Construction{
public:
Construction(){
//do the initialization thing and read the needed data from the file
}
SomeType sTArray[100];
};
__declspec(dllexport) Construction obj();
Run Code Online (Sandbox Code Playgroud)
现在它将被使用,程序员可以初始化对它的引用,然后使用如下的引用:
SomeType (&arrayRef)[100]=obj.sTArray;
Run Code Online (Sandbox Code Playgroud)
现在你觉得我在任何情况下都错了吗?
是的,你在某些时候为自己设置了一个非常令人讨厌的惊喜.
我建议您暂停初始化数组,直到第一次尝试访问它,这将要求您作为函数调用的结果间接公开数组:
struct Construction{
public:
Construction() : bInit(false) {};
SomeType* GetArray()
{
if(!bInit)
{
//do the initialization thing and read the needed data from the file
bInit = true;
}
return sTArray;
};
private:
SomeType sTArray[100];
bool bInit;
};
__declspec(dllexport) Construction obj();
Run Code Online (Sandbox Code Playgroud)
当然,这需要分成单独的头文件和实现文件.