小编Ani*_*nik的帖子

在构造函数中初始化数组数据成员

我有一个类对象数组,在类对象中我有另一个数组,我需要初始化为全零.代码编译并运行,但我的输出显示的是C而不是0.

从头文件:

class Cache {
private:
    int byte[16];
public:
    Cache();
    int getBytes(int);
    ~Cache();
};
Run Code Online (Sandbox Code Playgroud)

来自cpp文件

Cache::Cache()  
{
    byte[16]={0};
}

int Cache::getBytes(int j){
    return byte[j];
}
Run Code Online (Sandbox Code Playgroud)

来自其他cpp文件

for (int i = 0; i < 16; i++) 
{
    for (int j = 0; j < 16; j++)  //visual check of initializes main memory
    {
        cout << cache[i].getBytes(j) << " ";
}
}
Run Code Online (Sandbox Code Playgroud)

这是否正确设置?正如我所提到的,getBytes按预期返回'C'而不是'0'.

c++ arrays constructor

10
推荐指数
2
解决办法
1万
查看次数

标签 统计

arrays ×1

c++ ×1

constructor ×1