ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++翁恺学习17-const

2022-02-07 20:02:13  阅读:229  来源: 互联网

标签:const 17 int error ok 翁恺 day size


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; 对象是 constcip=&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
        ...
    }

标签:const,17,int,error,ok,翁恺,day,size
来源: https://blog.csdn.net/hymn1993/article/details/122808885

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有