码迷,mamicode.com
首页 > 其他好文 > 详细

const 学习笔记

时间:2018-04-12 19:57:19      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:指针   mes   AC   out   注意   int   bsp   div   ons   

#include<stdlib.h>
#include<iostream>

using namespace std;

int main(){
    // const 仅仅起到是否为常数的修饰
    // 普通变量
    int a=1;
    const int b=a;  // b定了,不能再赋值
    a=2;
    cout << b <<endl;

    // 指针时
    int *p=&a;
    const int *p1=p; // *p1定了,不能再给*p1赋值,但是p1可变,如p1=&c
    cout << *p1 <<endl;
    int * const p2=&a; // 注意此时const的顺序,此时p2不能变了
    a=2323;
    cout << *p2 <<endl;

    // 引用时
    int c=123;
    const int &y=c ; // y定了,所以y不能变了,但是c可变,y的值跟着变
    cout << y <<endl;
    c=2222;
    cout << y <<endl;
    system("pause");
    return 0;
}

 

const 学习笔记

标签:指针   mes   AC   out   注意   int   bsp   div   ons   

原文地址:https://www.cnblogs.com/pjishu/p/8809793.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!