const 详解

内容参考于://http://blog.csdn.net/eric_jo/article/details/4138548

#include<iostream>
#include<string>
using namespace std;
void f1(const int a,int b)
{
     //a++; 参数是常量不可变
     b++;
     cout<<a<<" "<<b<<endl;
}
void f2(const int *a,int *b)
{
    //(*a)++; 指针所指的内容是常量不可变
    (*b)++;
    cout<<*a<<" "<<*b<<endl;
    (*b)--;

}
void f3(int * const a,int *b)
{
       int p=3;
      // a=&p;  指针本身不可变
       b=&p;
       cout<<*a<<" "<<*b<<endl;
}
 void f4(const int &a,int & b)
 {
     //a++; 引用的内容不可变
     b++;
     cout<<a<<" "<<b<<endl;
 }

 const int f5()
 {
     return 7;
 }

 int temp=6;
 const int *f6()
 {
    // int temp=6;
     return &temp;
 }
int *const f7()
 {
     //int temp=6;
     return &temp;
 }
 class boy
 {
     int a;
     const int b;
     public:
     boy(int a,int b):a(a),b(b),gg(a),mm(b)
     {}
     void get()const
     {
         //boy::b++;   修饰为const 不能改变数据成员,
         //boy::a++;
         //put();     不能调用非const 函数
         pput();     //只能调用const 修饰的函数
         cout<<boy::a<<endl;
         cout<<boy::b<<endl;
     }
     void put()
     {
         cout<<"一把刷子"<<endl;
     }
     void pput()const
     {
         cout<<"一把刷子"<<endl;
     }
     int gg;
     const int mm;
 };

int main()
{
    //第一种 const 修饰常量
    //这两种方式是相同的
    cout<<"第一种:"<<" ";
    const int a=2;
    const int b=2;
    cout<<a<<endl<<b<<endl;
    //错误的 常量不能修改
    //cout<<a++<<endl<<++b<<endl;


    //第二种 const修饰指针
    //指针所指的内容是常量 不可变
    cout<<"第二种1:"<<" ";
    int x=2;
    int y=3;
    const int *p=&x;
    cout<<*p<<endl;
    //cout<<(*p)++<<endl; 指针所指的内容不可变
    p=&y;//但指针可变
    cout<<*p<<endl;
    cout<<"第二种2:"<<" ";
    int const *f=&y;
    cout<<*f<<endl;
    f=&x;////但指针可变
    cout<<*f<<endl;
    //cout<<(*f)++<<endl; 指针所指的内容不可变

    cout<<"第二种3:"<<" ";
    int mm=34;
    int gg=54;
    int * const m=&mm;
    cout<<*m<<endl;
    cout<<(*m)++<<endl;//指针所指的内容可变
    //m=&gg; 但指针不可变

    int jj=34;
    const int * const j=&jj;
    cout<<*j<<endl;
    //cout<<(*j)++<<endl; 两个都不可变
    //j=&mm;



    int a1=2,a2=2;
    cout<<"第三种1:"<<" ";f1(a1,a2);
    cout<<"第三种2:"<<" ";f2(&a1,&a2);
    cout<<"第三种3:"<<" ";f3(&a1,&a2);
    cout<<"第三种4:"<<" ";f4(a1,a2);


    f5();
    const int *a6=f6();
    cout<<"第四种1:"<<" ";
    cout<<(*a6)<<" ";
    //(*a6)++; 返回的指针所指的内容不可变,但指针可变
    a6++;
    cout<<(*a6)<<endl;

    int *const a7=f7();
     (*a7)++;
    //a7++;   指针所指的内容不可变 ,但指针可变
    cout<<"第四种2:"<<" ";
    cout<<(*a7)<<endl;


    boy  hh(3,5);
    cout<<"第五种1:"<<endl;
    hh.get();

    cout<<"第五种2:"<<endl;
    const boy pp(5,6);
    //pp.put(); 类修饰为const  不能访问非const函数  只能访问const 修饰的函数
    pp.pput();
    cout<<pp.gg<<" ";
    //cout<<(pp.gg)++<<" "; 不能修改数据成员
    cout<<pp.mm<<" ";
//    <<pp.mm++<<endl; 不能修改数据成员

    cout<<"第五种3:"<<endl;
    const boy *ppp=new boy(4,6);
   // ppp->put();   和上面一样的
    ppp->pput();

    cout<<"第五种4:"<<endl;
    boy * const pppp=new boy(4,6);
    pppp->put();
    pppp->pput();
    //pppp++;  指针不可变
    return 0;
}

原文地址:https://www.cnblogs.com/cs1003/p/2817445.html