[英]Initialize read only array only once
I have a class that needs to use some big arrays, initialized via some complex functions, that will be the same for every instance and will only be read after initialization. 我有一个需要使用一些大型数组的类,这些数组是通过一些复杂的函数初始化的,每个实例都相同,并且只有在初始化后才能读取。
I searched on SO and found some answers on initializing static arrays like this: 我在SO上进行搜索,找到了一些初始化静态数组的答案,如下所示:
char A::a[6] = {1,2,3,4,5,6};
But in my case I need to calculate the arrays at runtime via some function. 但就我而言,我需要在运行时通过某些函数计算数组。 (How) can I do it?
(我该怎么做?
Re 回覆
” will be the same for every instance and will only be read after initialization
”对于每个实例都是相同的,并且只有在初始化后才能读取
Producing a value is the job of a function. 产生值是功能的工作。
Just define a function that returns the data you need. 只需定义一个返回所需数据的函数即可。
You can use it to initialize a static data member (or whatever). 您可以使用它来初始化静态数据成员(或其他)。 For a header only module, if that's relevant, you will need to employ solution to the "inline data" problem, eg a Meyers' singleton (a function that returns a reference to a local static variable).
对于仅标题的模块,如果相关的话,您将需要采用解决“内联数据”问题的解决方案,例如Meyers的单例(返回对局部静态变量的引用的函数)。 Like this:
像这样:
#include <vector>
namespace my {
using std::vector;
inline
auto squares()
-> vector<int>
{
vector<int> result;
for( int i = 1; i <= 12; ++i ) { result.push_back( i*i ); }
return result;
}
class A
{
private:
static
auto a()
-> const vector<int>&
{
static const vector<int> the_values = squares();
return the_values;
}
public:
A(){}
};
} // namespace my
You can't use {} sintaxis in execution time, you can use a method: 您不能在执行时使用{} sintaxis,可以使用以下方法:
class A
{
static vector<char> a;
//...
public:
static void initStatic();
}
void A::initStatic()
{
a.resize( /*put the size here... */);
for (auto& x : a)
x = //something...
}
vector reference: http://en.cppreference.com/w/cpp/container/vector 矢量参考: http : //en.cppreference.com/w/cpp/container/vector
If you aren't using vectors, this works. 如果您不使用向量,则可以使用。 The reason I let
A::initialize
do the work, rather than just calling one of these externally defined functions, is that we can and should expect the data member a
to be private. 我之所以让
A::initialize
完成这项工作,而不仅仅是调用这些外部定义的函数之一,是因为我们可以而且应该期望数据成员a
是私有的。
//Declare a function pointer type, so you can pass it into A's
//an initialization function takes in the array and its size
typedef void (*initFunction) (char A[], int arraySize);
//see http://www.cprogramming.com/tutorial/function-pointers.html
// for more on function pointers
class A
{
public:
void initialize (initFunction myInitFunction);
...
private:
char a[ARRAYSIZE];
};
void A::initialize (initFunction myInitFunction)
{
(*myInitFunction) (a, ARRAYSIZE);
}
...
A myA;
myA.initialize (yourArrayInitializingFunction);
Or maybe your initialization functions don't take in arrays and initialize them, but return arrays: 或者,也许您的初始化函数不接受数组并对其进行初始化,而是返回数组:
class A
{
public:
void initialize (const char* aInit);
...
};
void A::initialize (const char* aInit)
{
for (int i = 0; i < ARRAYSIZE: ++i)
a[i] = aInit[i];
}
...
A myA;
myA.initialize (yourArrayReturningFunction ());
If you're using vectors, code is simpler: 如果您使用向量,则代码会更简单:
class A
{
public:
void initialize (const vector<char>& aInit) { a = aInit; }
...
private:
vector<char> a;
};
My suggestion: 我的建议:
Instead of using a static member variable, use a static member function to provide access to the array. 代替使用静态成员变量,可以使用静态成员函数来提供对数组的访问。
In the static member function, create a static function variable that can be populated the first time it is needed. 在静态成员函数中,创建一个静态函数变量,该变量可以在第一次需要时填充。
Here's what I am thinking of: 这就是我的想法:
char* A::getArray()
{
static char a[6] = {0};
static bool inited = false;
if ( !inited )
{
// Initialize the array elements
a[0] = ... ;
...
a[5] = ... ;
inited = true;
}
return a;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.