首页 > 试题广场 >

下列const使用方法错误的是( &nb...

[单选题]
下列const使用方法错误的是(      )
  • const int Val = 10; Val = 20;
  • class A   { const int SIZE = 100; int array[SIZE];};
  • class A { protected:  static int const Inity; };
  • int a=7;const int *aPtr;aPtr = &a;
pointer to const可以指向非const
发表于 2019-10-08 16:45:42 回复(0)
const int a = 10; 	//编译可通过,和int const a = 10; 表达的效果是一样的
a = 20; 			//编译不通过

const int a; 		//编译不通过,必须初始化

//指针常量,指向不能修改,指向的内存中的值可以修改,相当于数组名;
int b = 100;
int *const p; 		//编译报错,必须初始化
int *const p = &b; 	//编译通过
int *const p = &a;	//编译不通过,因为a是常量

//常量指针,指针指向可以修改,但指向的值不能修改,此时该指针也叫只读指针
int const *p;		//编译能通过,只是p为野指针
int const *p = &a;	//编译可通过
int const *p = &b;	//编译可通过
*p = 200; 			//不管p指向a还是b,编译都不会通过

//常量指针常量
const int *const pp;		//编译不通过,必须初始化
const int *const pp = &a; 	//编译通过
const int *const pp = &b; 	//编译通过,此时b可以改变值,而且pp指向的值同步改变,但不能通过pp修改b的值

发表于 2021-06-30 11:32:13 回复(0)
B选项中,const成员不应该是在参数列表中初始化的吗?感觉B也不对。
发表于 2020-02-27 11:13:35 回复(0)
A:符号常量在赋予初值后,不可改变其值
发表于 2021-05-14 13:25:56 回复(0)
const 声明的类型变量是“只读”,不可以写; 另外,D项经编码验证后,aPtr是常量指针指向一个int常量,通过aPtr=&a指向a,但是没有改变a的属性,a还是可读可写的。
编辑于 2019-09-03 10:26:15 回复(0)
发表于 2022-09-02 18:17:13 回复(0)
C不是要初始化吗?
发表于 2022-09-02 16:59:25 回复(0)
const所修饰的,不能更改意思,而a对其值修改,所以错了~
发表于 2021-05-05 15:50:45 回复(0)
个人认为B选项在C++ 11后可以在类内直接初始化const成员变量,但后面定义的array数组是非法的,可以将SIZE类型改为static const int,并且SIZE要在定义的时候就初始化为一个非负值
发表于 2020-06-02 17:09:01 回复(0)
C不需要初始化吗
发表于 2020-04-14 08:51:06 回复(0)
A两个错误吧,1是变量不能被定义两次,2是中间要用逗号不是分号。
发表于 2020-01-30 23:44:21 回复(1)
const 限制变量不能改变?
发表于 2019-08-20 09:44:10 回复(0)