gpt4 book ai didi

c++ - 在类中初始化 const 数组

转载 作者:太空宇宙 更新时间:2023-11-04 15:24:11 24 4
gpt4 key购买 nike

在 Visual C++ 中,如何在类中初始化常量数组?

这是一个如何在类之外进行的示例:

const char k_colors[] = 
{
'R',
'G',
'B',
};

现在我需要如何更改它? (我试过在它前面放static,没用)

编辑:你说得对,我应该只使用单个字符。

最佳答案

如果你希望它是静态的,你需要在类外初始化它:

class foo
{
public:
static const char k_colors[3];
foo() { }

};

const char foo::k_colors[] = {'a', 'b', 'c'};

此外,您可能希望它是一个 const char *[],因为它看起来像您正在尝试初始化字符串,所以它应该是:

const char *foo::k_colors[] = {"Red", "Green", "Blue"};

关于c++ - 在类中初始化 const 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12741629/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com