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

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

问题描述

我刚刚发现,以下是无效的。

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?

在此先感谢;

推荐答案

最好的地方是在源文件

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

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

您可以在类定义中初始化整数类型,如你试图做的;所有其它类型必须定义和类定义之外初始化,并且只有一次。

You can initialise integer types in the class definition like you tried to do; all other types have to be defined and initialised outside the class definition, and only once.

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

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