关于 const,以下正确的是

来源:互联网 发布:手机可以禁止安装软件 编辑:程序博客网 时间:2024/06/10 09:08

Please choose the right statement about constusage:

正确答案: A B C   你的答案: C E (错误)

const int a; //const integer
int const a; //const integer
int const *a; //a pointer which point to const integer
const int *a; //a const pointer which point to integer
int const *a; // a const pointer which point to integer
  • 添加笔记
  • 收藏
  • 纠错


ABC
对于A和B,int const 和 const int 可以颠倒位置,意义不变
CDE都表示指向const int 的指针,而int *const a 才表示指向int的const指针

class A{private:  const int a = 1;  //error: 只有静态整型常量可以在类中初始化  static const int h = 1;         //ok  static const double h1 = 1.0;   //error: 只有静态整型常量才能在类中初始化  const int b,c;  int d;  int &e=b;  //error:只有静态变量才能在类中初始化。但引用必须定义时初始化,怎么办呢?在初始化列表中初始化  int &f;  static int g;public:  A():c(1),f(d){  //ok:常量,引用只能在构造函数的初始化列表中初始化    b=1;//error:常量只能在构造函数的初始化列表中初始化    d=2;//ok:变量可以在这儿初始化,没问题    g=1;//error:静态变量在构造函数中不能初始化  } };
int A::a; //加上这一句后,上边的g = 1;这一行就没问题了。静态变量必须在类定义体的外部定义一次。          //注意,这儿不能写成static int A::a; static关键字要去掉。



0 0
原创粉丝点击