如何声明常量 [英] how to declare constants

查看:110
本文介绍了如何声明常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,在一本高效的c ++书中,我看到常量已声明为"const char * const authoeName ="Scott Meyers".有人可以告诉我此语句的含义吗?为什么要使用两个关键字"const"?请帮帮我....
预先谢谢您...

解决方案

此处const在*之前和*之后.因此,地址和指向authoeName 的值都不能更改.意味着,
const char* const authoeName = "Scott Meyers";之后,以下语句将给出编译错误,

*authoeName = "New author";
authoeName  = &NewChar; 



同样在这种情况下,您应该初始化authoeName.否则编译器会报错.

另请检查此链接以获取更多信息
http://www.possibility.com/Cpp/const.html [ const char * constantString = " ; constantString ++; // 可以做到这一点 // 无法更改字符串的内容, // 下一行将无法编译: // contstantString [0] ='a'; char * const constantPointer = " 跨度>; constantPointer [ 0 ] = ' !' ; // 可以做到这一点 // 无法修改指针, // 下一行将无法编译: // constantPointer ++; // 无法修改内容或指针: 常量 char * const readOnly = " 别碰!" ;



如果还有其他需要解释的地方?

—SA


要了解这一点,您需要了解
之间的区别 1.指向常量的指针 2.常量指针.

1.指向常量的指针

int n1 = 10;
int* pn = &n1;



假设 n1 的地址为0x1001.
当我们说 pn 时,它等于0x1001.我们说 * pn 等于10;
请参阅以下

int n1 = 10;
const int* pn = &n1;


这是一个指向常量整数的指针.
表示指针不是常量.但是它指向的是一个常量;

*pn = 10; // will cause an error in this case. because we are trying to change the value of what pn is pointing to.

int n2 = 20;
pn = &n2; /* This will not cause any errors. becuse we are changing the pointer value. pn is pointing to n2 instead of n1.No problem at all*/


2.常量指针
是上述的另一种方式

int n1 = 10;
int* const pn = &n1;
*pn = 10; // No error

int n2 = 20;
pn = &n2; // will cause an error


牢记以上所有内容,我们可以说
const char * const authoeName ="Scott Meyers"/*是指向常量字符的常量指针.指针及其指向的都是常量.*/

const char* const authoeName = "Scott Meyers";
char* bookname = "Effective c++";

authoeName = bookname; // Error
*authoeName = *bookname; // Error


两种更改都会导致错误


hi all,in an effecitve c++ book,i have seen that the constants has been declared as " const char* const authoeName = "Scott Meyers". can anybody please tell me the meaning of this statement?why we should use two ''const'' keyword?please help me....
thank you in advance...

Here const is coming before the * and after the *. so, neither the address nor the value pointed authoeName can be changed.Means,
const char* const authoeName = "Scott Meyers"; after this the following statements will give compilation errors,

*authoeName = "New author";
authoeName  = &NewChar; 



Also you should initialize authoeName in this case.otherwise compiler will give error.

Also check this link for more info
http://www.possibility.com/Cpp/const.html[^]


A constant, some object which you cannot modify. This is a very important feature of practically all languages. The technical representation of constants of constants is application-dependent. Parameters of functions can be constant as well.

When it comes to pointers, you need to know the difference between the following cases:

const char* constantString = "My constant string";
constantString++; //can do this
// cannot change the content of the string,
// next line won't compile:
//contstantString[0] = 'a';

char* const constantPointer = "My constant pointer";
constantPointer[0] = '!'; //can do this
// cannot modify pointer,
// next line won't compile:
//constantPointer++;

//cannot modify content or pointer:
const char* const readOnly = "Don't touch!";



If there something else you need to get explained?

—SA


to understand this you need to understand the difference between
1. pointer to constant
2. constant pointer.

1. pointer to constant

int n1 = 10;
int* pn = &n1;



suppose the address of n1 is 0x1001.
when we say pn, it is equal to 0x1001. when we say, *pn it is equal to 10;
see the below

int n1 = 10;
const int* pn = &n1;


this is a pointer to a constant integer.
means the pointer is not constant. but to what it points to is a constant;

*pn = 10; // will cause an error in this case. because we are trying to change the value of what pn is pointing to.

int n2 = 20;
pn = &n2; /* This will not cause any errors. becuse we are changing the pointer value. pn is pointing to n2 instead of n1.No problem at all*/


2. constant pointer
is the other way round of above

int n1 = 10;
int* const pn = &n1;
*pn = 10; // No error

int n2 = 20;
pn = &n2; // will cause an error


keeping all the above in mind we can say
const char* const authoeName = "Scott Meyers" /* is a constant pointer to a constant character. both the pointer and what it points to is constant.*/

const char* const authoeName = "Scott Meyers";
char* bookname = "Effective c++";

authoeName = bookname; // Error
*authoeName = *bookname; // Error


both kind of alteration will cause an error


这篇关于如何声明常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆