0
点赞
收藏
分享

微信扫一扫

C++翁恺学习17-const

一枚路过的程序猿 2022-02-07 阅读 121

const

  • declares a variable to have a constant value.

constants

  • constants are variables 
    • observe scoping rules
    • declared with "const" type modifier(修饰)

compile time constants

  • value must be initialized
  • unless you make an explicit extern declaration
  • 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

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

pointers and constants

int i;const int ci=3;
int *ip;ip=&i;ip = &ci;  //error
const int *cip; 对象是 constcip=&i;cip = &ci;
  • remember

string literals 字符串常量

  • 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:

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 indicates only one per class (not one per object)

  • or use "anonymous enum" hack

    class HasArray{
        enum{size = 100};
        int array[size];//ok
        ...
    }
举报

相关推荐

C++翁恺学习16-内联函数

C++翁恺学习12-对象组合

C++翁恺学习32-异常基本概念

C++翁恺学习33-异常的抛出和捕捉

整数分解:C/C++_翁恺老师

翁恺c语言数字特征值

翁恺C语言第二周2.1打卡

0 条评论