静态常量初始化结构数组在C ++类 [英] Static Const Initialised Structure Array in C++ Class
问题描述
我明白,如果我想在C类命名空间的常量数组++,我不能做的:
I understand if I want a const array in a class namespace in C++ I cannot do:
class c
{
private:
struct p
{
int a;
int b;
};
static const p pp[2];
};
const c::p pp[2] = { {1,1},{2,2} };
int main(void)
{
class c;
return 0;
}
我必须做的:
class c
{
public:
struct p
{
int a;
int b;
};
static const p pp[2];
};
const c::p pp[2] = { {1,1},{2,2} };
int main(void)
{
class c;
return 0;
}
但这需要p和PP是公开的,我希望他们是私有的。是否有C ++没有办法初始化私有静态数组?
But this requires "p" and "pp" to be public, when I want them to be private. Is there no way in C++ to initialise private static arrays?
编辑:-------------------
感谢您的答案。此外,我想这个类是一个库,头文件只,由一个主项目中使用。包括多个文件时,包含在错误的多重定义下面的初始化器的效果。
------------------- Thanks for the answers. In addition I want this class to be a library, header files only, for use by a main project. Including the following initialiser results in " multiple definition of " errors when included by multiple files.
const c::p c::pp[2] = { {1,1},{2,2} };
我该如何解决呢?
How can I solve this?
推荐答案
您先code段工作正常。你只需要将其更改为:
Your first code snippet works fine. You just need to change it to:
const c::p c::pp[2] = { {1,1},{2,2} };
这篇关于静态常量初始化结构数组在C ++类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!