在头文件中初始化常量静态数组 [英] Initializing Constant Static Array In Header File

查看:45
本文介绍了在头文件中初始化常量静态数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发现以下内容无效.

I have just found out that the following is not valid.

//Header File
class test
{
    const static char array[] = { '1', '2', '3' };
};

初始化它的最佳位置在哪里?

Where is the best place to initialize this?

推荐答案

最好的地方是在源文件中

The best place would be in a source file

// Header file
class test
{
    const static char array[];
};

// Source file
const char test::array[] = {'1','2','3'};

您可以像尝试那样在类声明中初始化整数类型;所有其他类型都必须在类声明之外初始化,并且只能初始化一次.

You can initialize integer types in the class declaration like you tried to do; all other types have to be initialized outside the class declaration, and only once.

这篇关于在头文件中初始化常量静态数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆