C++ 类中声明并定义静态常量字符串数组属性

#include <iostream>
using namespace std;

 

class Person {
   public:
    int id = 200;
    string name = "qiumc";
    // 在类中声明并定义静态常量字符串数组
    // char*改为string类型会报错
    // 没有constexpr的修饰会报错
    static constexpr const char* arrayAttribute[] = {"Hello123", "World123"};
};

 

int main(int argc, char const* argv[]) {
    Person p;
  
    // cout << Person::arrayAttribute << endl; 直接这种写法会报错,这是因为constexpr是在编译的时候求值,编译后根本就没有Person::arrayAttribute这个符号
    cout << Person::arrayAttribute[0] << endl;
    cout << Person::arrayAttribute[1] << endl;
    return 0;
}
 
 
 

原文链接: https://www.cnblogs.com/qiumingcheng/p/15520107.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    C++ 类中声明并定义静态常量字符串数组属性

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/214752

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月13日 上午2:28
下一篇 2023年2月13日 上午2:28

相关推荐