const
- declares a variable to have a constant value.
constants
- constants are variables
- observe scoping rules
- declared with "const" type modifier(修饰)
compile time constants
const int bufsize = 1024;
- value must be initialized
- unless you make an explicit extern declaration
enxtern const int bufsize;
- complier won't let you change it
- compile time constants are entries in compiler symbol table,not really variables
run-time constants
- const value can be exploited
const int class_size = 12;
int finalGrade[class_size];//ok
int x;
cin>>x;
const int size = x;
double classAverage[size];//error
编译器不知道x的值,因为编译器不知道需要分配内存的大小。
pointer and const
char *const q = "abc";//q is const
*q = "c"//ok
q++;//error 指针是const
const char *p = "abcd";//(*p) is a cosnt char
*p = "b";//error 指针指的内存的东西是 const
p++; // ok 下一个内存的东西也是 const 了
quiz: what do these mean?
cosnt person* p = &p1; // 对象是 const
person const* = &p1; // 对象是 const
person *const p= &p1; // 指针是 const
*的前面还是后面。 * 在 const 前, 指针是 const; * 在 const 后,对象是 const
pointers and constants
int i; | const int ci=3; | |
---|---|---|
int *ip; | ip=&i; | ip = &ci; //error |
const int *cip; 对象是 const | cip=&i; | cip = &ci; |
-
remember
*ip=54; //always legal since ip points to int *cip=54; //never legal since cip points to const int
string literals 字符串常量
char* s = "hello,world";
-
s is a pointer initialized to point to string constant
-
this is actually a "const char* s" but compiler accepts it without the const
-
don't try and change the character values(it is undefined behavior)
if you want to change the string ,put it in an array:
char s[] = "hello,world!"; // 数组在栈中
C++中三种内存模型:
本地变量,在栈中;
全局变量在全局数据区里,全局变量中的这种常量(“hello,world”)在代码段里,代码段不可写的;
new出来的东西在堆中;
conversions
-
can always treat a non-const value as const
void f(const int* x){...} int a = 15; f(&a);//ok const int b = a; f(&b);//ok b=a+1;//error
-
you cannot treat a constant object as non-constant without an explicit cast(const_cast)
passing by const value
void f1(const int i){ i++;//illegal -- compile-time error }
returning by const value?
int f3(){return 1;} const int f4(){return 1;} int main(){ const int j = f3();//work fine int k = f4(); //but this works fine too }
passing and returning addresses
-
passing a whole object may cost you a lot.it is better to pass by a pointer.but it's possible for the programmer to take it and modify the original value
-
in fact ,whenever you're passing an address into a function,you should make it a const if at all possible
constant objects
-
what if an object is const?
const Currency the_raise(42,38);
-
what members can access the internals?
-
how can the object be protected from change?
const member functions
-
cannot modify their
int Data::set_day(int d){ //...error check d here... day = d;//ok,non-const so can modify } int Data::get_day() const{ day++; //error modifies data member set_day(12); //error calls non-const member return day;//ok }
const member function usage
-
repeat the const keyword in the definition as well as the declaration
int get_day() const;//this ------ is const int get_day() const {return day;}
-
functon members that do not modify data should be declared const
-
const member function are safe for const objects
constant in class
class A{ const int i; };
- has to be initialized in initializer list of the cosnstructor
compile-time constants in classes
class HasArray{ const int size; int array[size]; //error,不能作为数组的大小, 1.使用static 2. 枚举 ... }
-
make the const value static:
static const int size = 100;
static indicates only one per class (not one per object)
-
or use "anonymous enum" hack
class HasArray{ enum{size = 100}; int array[size];//ok ... }