【发布时间】:2010-06-17 22:49:58
【问题描述】:
我有一个头文件,其中包含一个静态字符数组的成员变量声明:
class ABC
{
public:
static char newArray[4];
// other variables / functions
private:
void setArray(int i, char * ptr);
}
在 CPP 文件中,我将数组初始化为 NULL:
char ABC::newArray[4] = {0};
在ABC构造函数中,我需要用运行时构造的值覆盖这个值,比如整数的编码:
ABC::ABC()
{
int i; //some int value defined at runtime
memset(newArray, 0, 4); // not sure if this is necessary
setArray(i,newArray);
}
...
void setArray(int i, char * value)
{
// encoding i to set value[0] ... value [3]
}
当我从这个函数返回并打印修改后的 newArray 值时,它会打印出比数组声明中指定的 4 多得多的字符。
任何想法为什么会这样。 我只想将 char 数组设置为 4 个字符,仅此而已。
谢谢...
【问题讨论】:
标签: c++