在C ++中初始化一个静态成员(数组) [英] Initialize a static member ( an array) in C++

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

问题描述

我打算创建一个只有静态成员和静态函数的类。成员变量之一是数组。是否可以在不使用构造函数的情况下初始化它?我现在有很多链接错误...

I intended to create a class which only have static members and static functions. One of the member variable is an array. Would it be possible to initialize it without using constructors? I am having lots of linking errors right now...

class A
{
public:
  static char a[128];
  static void do_something();
};

如何初始化[128]?为什么不能像在C中一样直接指定它的值来初始化[128]?

How would you initialize a[128]? Why can't I initialize a[128] by directly specifying its value like in C?

a[128] = {1,2,3,...};


推荐答案

您可以在.cpp文件:

You can, just do this in your .cpp file:

char A::a[6] = {1,2,3,4,5,6};

这篇关于在C ++中初始化一个静态成员(数组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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