How to declare a static const char* in your header file?

后端 未结 9 1086
盖世英雄少女心
盖世英雄少女心 2021-01-31 13:26

I\'d like to define a constant char* in my header file for my .cpp file to use. So I\'ve tried this:

private:
    static const char *SOMETHING = \"sommething\";         


        
9条回答
  •  太阳男子
    2021-01-31 14:26

    To answer the why question, integral types are special in that they are not a reference to an allocated object but rather values that are duplicated and copied. It's just an implementation decision made when the language was defined, which was to handle values outside the object system and in as efficient and "inline" a fashion as possible.

    This doesn't exactly explain why they are allowed as initializors in a type, but think of it as essentially a #define and then it will make sense as part of the type and not part of the object.

提交回复
热议问题