用数组初始化 class

Initialize class with array

我在这方面发现了很多问题,但 none 似乎与我要找的相符。

目标是初始化一个常量class实例,使其完全放入微控制器的闪存中,不占用任何内存。

这是一个无效的例子:

   class Message {

            public:

                    const int _addr;
                    const char _data[ 4 ];

                    Message( int addr, const char data[4] )
                                    : _addr( addr )
                                    , _data( data )
                                    {}
    };

    const Message m[] = { 
            Message( 0x123, { 4, 5, 6, 7 } ),
            Message( 0x234, { 5, 6, 7, 8 } )
    };

使用 g++11 失败并显示消息:

    g++     test.cpp   -o test
    test.cpp: In constructor ‘Message::Message(int, const char*)’:
    test.cpp:10:20: error: incompatible types in assignment of ‘const char*’ to ‘const char [4]’

使用不同的括号组合失败并出现其他错误...

拥有指向 Flash 其他部分的指针将起作用并将所有内容都放入 Flash 中,但这不是我想要的。 (而且还需要额外的space)

如果我用 C 来做,我会这样做:

typedef struct {                                                                                                                                                         
                                                                                                                                                                         
    const int _addr;                                                                                                                                                     
    const char _data[ 4 ];                                                                                                                                               
                                                                                                                                                                         
} Message;                                                                                                                                                               
                                                                                                                                                                         
const Message m[] = {                                                                                                                                                    
    { 0x123, { 4, 5, 6, 7 } },                                                                                                                                           
    { 0x234, { 5, 6, 7, 8 } }                                                                                                                                            
};                                                                                                                                                                       

类似这样的方法可行:

class Message {

            private:

                    const int _addr;
                    const char * _data;
public:
                    Message( int addr, const char * data )
                            : _addr( addr )
                            , _data( data )
                            {}
    };


    char mm1[] = {4, 5, 6, 7};
    char mm2[] = {5, 6, 7, 8};
    const Message m[] = { 
            Message( 0x123, mm1 ),
            Message( 0x234, mm2 )
    };
    

如解释的那样(感谢 taiBu 在问题评论中发布链接)

Your constructor argument [data] is, actually, not an array! Yes, I know it looks like one, because you wrote [char data[4]]. But, actually, it's [char* data].

...

So, the error message is telling you that you cannot assign a pointer to an array. Fair enough. Doesn't really matter anyway, since the language also doesn't let you assign arrays to arrays. Lol.

但是你可以使用 aggregate initialization:

做这样的事情
struct Message {
    const int _addr;
    const char _data[ 4 ];
};

const Message m[] = { 
    Message{ 0x123, { 4,5,6,7}},
    Message{ 0x234, { 5,6,7,8}}
};

live demo