通过代码学习关键字const

最近要面试,就来回顾一些基本知识。今天简略总结一下const的用法。

代码实践

//
//  main.cpp
//  learn_const
//
//  Created by cslzy on 16/8/23.
//  Copyright © 2016年 CY. All rights reserved.

//  参考:http://www.cnblogs.com/jiabei521/p/3335676.html

#include <iostream>
using namespace std;

class test{
public:
    void print();
    void print() const;
};
void test::print() const
{
    cout<<"constant print function"<<endl;
}

void test::print()
{
    cout<<"not a constant print function"<<endl;
}

class A{
public:
    A(int a);
    static void print();
    const int bb;
    const int &r;
    static int aa; // 整个文件中只声明一次,但是可以多次修改。
private:
    static const int count;
};

int A::aa = 0;// 对类A的对象aa进行初始化赋值。整个文件中,所有A类的对象里面的aa变量只初始化一次。
const int A::count = 20; // 默认省略static关键字

A::A(int a):bb(a), r(bb){ // 而常量bb需要在初始化列表中声明。对于常量型成员变量和引用型成员变量,必须通过参数化列表的方式进行初始化
    aa += 1;
    //count + =1;
}
// 下面的就不对
//A::A()
//{
//    bb = 0;
//}

void A::print()
{
    cout<<"count = "<<count<<endl;
    cout<<"aa = "<<aa<<endl;
//    cout<<"bb = "<<bb<<endl;
}

int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    // 1. const int 与 int 赋值问题
    int i = 42;
    const int &a = 42;
    const int &b = a + i; //这里的是常数引用变量,b是(a + i)这个值的一个引用,不能通过b对(a + i)进行修改。变量b自带了一把'锁'不允许外界通过变量b对b反向的值进行修改。但这并不意味着b指向的内容不能被修改。
    const int *c = &i;
    cout<<"const int *c: "<<*c<<endl;
    i++;
    cout<<"i++"<<endl;
    cout<<"const int *c: "<<*c<<endl;
//    *c += 1;// error, const 的含义就是不能通过该变量对内存中的数据进行修改。

    // 2. 将一个const对象的地址赋给一个普通的非const指针也会导致编译错误
    //    但反过来没有问题。
    //    去掉下面的const试试。
    const int *aconst = &a; // const int 赋值给 const int 指针
    const int *iconst = &i; // int 赋值给 const int 指针
//    int *aconst = &a; // 将const int* 赋值给 int* 是不行的。
    cout<<b<<endl;

    // 3. 给指向const量数组的指针定义的时候也要初始化。
//    const int *pci_error = new const int[100];
    const int *pci_ok = new const int[100]();
    cout<<*pci_ok<<endl;
    cout<<"3: "<<*pci_ok<<endl;

    // 4. const 可用作重载的关键字。
    //    要调用const函数,应当声明const对象。
    test atest;
    atest.print();
    const test btest{}; // 这里的{}是调用默认的构造函数,进行初始化。因为const对象声明的时候就应该被初始化。
//  test const btest{}; // 这样的声明方法也是可以的。
    btest.print();
    const test *ctest = new const test() ;
    ctest->print();

    // 5. const 指针对象。可以通过该指针修改该指针指向的对象,但不能修改这个指针。
    cout<<i<<endl;
    int *const itest = &i;
    *itest += 1;
    //itest ++; // 提示:Read-only variable is not assignable
    cout<<i<<endl;

    // 6. const 与 static 在类中的使用
    A aa(10);
    A::print();
    aa.print();
    cout<<aa.bb<<endl;
    A cc(15);
    cc.print();
    cout<<cc.bb<<endl;
    cout<<aa.aa<<endl;
    aa.aa += 1;
    cout<<"class A's aa in object aa:"<<aa.aa<<endl;
    cout<<"class A's aa in object cc:"<<cc.aa<<endl;
    aa.aa += 1;
    cout<<"class A's aa in object aa:"<<aa.aa<<endl;
    cout<<"class A's aa in object cc:"<<cc.aa<<endl;
    // 相比与static,从字面意思理解
    // static是静止的意思,它只能在一个文件中被定义一次,下次再遇到它的定义
    // 哪怕不在一个对象的函数里,它的值仍然是该次定义之前的值。
    // 文章里有句话说的很好,static变量属于类,不属于对象。


    // 7. 指向常量的常量指针
    const int *const pointer = &a; // 这个很有意思这样pi_ptr首先是一个const指针,然后其指向一个const对象~~~
    cout<<*pointer<<endl;

    return 0;
}

自己动手跑一下程序,做一些简单的测试,对语法的理解会比单纯看书来得更快些。
个人感觉:理解const对象,还是要看const关键字限定在哪个层次上。

参考

C++中const关键字的使用方法,烦透了一遍一遍的搜,总结一下,加深印象!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值