zoukankan      html  css  js  c++  java
  • 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;
    }
     
     
     
  • 相关阅读:
    Java1:Chapter3
    css3圆角和阴影效果
    css3兼容各版本浏览器前缀
    DOM
    数组方法
    Math方法
    JSON
    字符串方法
    日期对象
    定时器
  • 原文地址:https://www.cnblogs.com/qiumingcheng/p/15520107.html
Copyright © 2011-2022 走看看