简体   繁体   English

声明类似于C风格的数组(C ++)

[英]Declaring arrays similar to C style (C++)

In C a programmer can declare an array like so: 在C中,程序员可以像这样声明一个数组:

unsigned char Fonts[2][8] {
    [1] = {0, 31, 0, 31, 0, 31, 0, 31}
};

And element [0] is likely random bits. 元素[0]可能是随机位。 Is there a similar way in C++? 在C ++中是否有类似的方法?

This works in C++: 这适用于C ++:

unsigned char foo [2][8] = {
        {},
        {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}
};

Here, foo[0] is zero-initialized as defined by the C++ standard ( §8.5.5 - default initialization for POD types is zero-initialization). 这里, foo[0]是由C ++标准定义的零初始化( §8.5.5 - POD类型的默认初始化是零初始化)。 For non-POD-types the default constructor is called. 对于非POD类型,将调用默认构造函数。

You can do this: 你可以这样做:

unsigned char Fonts[2][8] = {
    {0},
    {0, 31, 0, 31, 0, 31, 0, 31}
};

Why not just do 为什么不这样做呢

unsigned char Fonts[2][8] {
    {0, 0, 0, 0, 0, 0, 0, 0},
    {0, 31, 0, 31, 0, 31, 0, 31}
};

?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM