C++ 之const详解

const详解

一、const修饰成员变量

1.只有一个cosnt时,如果const位于*的左侧:表示指针所指的数据常量,不能通过该指针修改实际数据;指针本身是变量,可以指向其他内存单元。

2.只有一个const时,如果const位于*右侧:表示指针本身时常量,不能指向其他内存单元;所指向的数据可以修改

3.如果有两个const位于*的左右两侧,表示指针和指针所指向的数据都不能修改

//1.const修饰成员变量
void ConstDemo1()
{
    int num1 = 1024;
    const int num2 = num1;

   //assignment() of read-only variable 'num2'
   // num2 = 2048;

    cout<< num2 <<endl;

    const int * ptr_num1 = &num1;
    int const * ptr2_num1= &num1;
    ptr_num1 = &num2;   //合法
    //*ptr_num1 = 1234;   //不合法
    //const修饰指针变量时:
    //1.只有一个cosnt时,如果const位于*的左侧:表示指针所指的数据常量,不能通过该指针修改实际数据;
    //  指针本身是变量,可以指向其他内存单元
    //2.只有一个const时,如果const位于*右侧:表示指针本身时常量,不能指向其他内存单元;所指向的数据可以修改
    int * const ptr3_num1 = &num1;
    //ptr3_num1 = ptr2_num1;//不合法
    //3.如果有两个const位于*的左右两侧,表示指针和指针所指向的数据都不能修改

}

二、const修饰函数参数

传递来的参数num在函数体内不可改变,与修饰时的性质一致

//2.const修饰函数参数
void ConstDemo2(const int num)
{
    //num = 123;//传递来的参数num在函数体内不可改变,与修饰时的性质一致
}

const修饰引用时,不能修改引用对象的任何成员 - 好处是可以保护传递参数:不需要一个新的参数副本(copy)
使用const传递对象的引用时,可以起到不copy对象的目的(节省省略)

class Computer{
public:
    Computer(int core){this->m_core = core;}

    void buy(){}

    void buy(int core)
    {

    }

    /**修改电脑的核心频率*/
    void SetCore(int core){this->m_core = core;}
    int GetCore()const{return m_core;}
private:
    int m_core;//cpu主频
};
void ConstDemo3(const Computer & computer)
{
    //const修饰引用时,不能修改引用对象的任何成员 - 好处是可以保护传递参数:不需要一个新的参数副本(copy)
    // passing 'const Computer' as 'this' argument of 'void Computer::buy(int)' discards qualifiers [-fpermissive]|
    //computer.buy(123);//const成员函数不能调用非const成员函数
    //使用const传递对象的引用时,可以起到不copy对象的目的(节省省略)
}

三、const修饰返回值

强调:使用const修饰引用类型的一个常见的原因是:提高效率

如果函数要返回局部对象,就应该直接返回这个对象,而不要返回对象的引用
在可以返回对象,也可以返回引用时,就应该首选引用,因为效率高

class Computer{
public:
    Computer(int core){this->m_core = core;}

    void buy(){}

    void buy(int core)
    {

    }

    /**修改电脑的核心频率*/
    void SetCore(int core){this->m_core = core;}
    int GetCore()const{return m_core;}
private:
    int m_core;//cpu主频
};
const Computer & GetMax(const Computer & com1, const Computer & com2){
    if(com1.GetCore()>com2.GetCore())
    {
        return com1;
    }
    return com2;
}

四、cosnt修饰函数

说明函数不会修饰成员变量的值

class TestClass{
public:
    int value;
    void ModifyValue()const{
        value = 1111;//会报错
    }
};

附件:

ConstDemo.cpp

#ifndef CONSTDEMO_H_INCLUDED
#define CONSTDEMO_H_INCLUDED

#include<iostream>
using namespace std;

//1.const修饰成员变量
void ConstDemo1()
{
    int num1 = 1024;
    const int num2 = num1;

   //assignment() of read-only variable 'num2'
   // num2 = 2048;

    cout<< num2 <<endl;

    const int * ptr_num1 = &num1;
    int const * ptr2_num1= &num1;
    ptr_num1 = &num2;   //合法
    //*ptr_num1 = 1234;   //不合法
    //const修饰指针变量时:
    //1.只有一个cosnt时,如果const位于*的左侧:表示指针所指的数据常量,不能通过该指针修改实际数据;
    //  指针本身是变量,可以指向其他内存单元
    //2.只有一个const时,如果const位于*右侧:表示指针本身时常量,不能指向其他内存单元;所指向的数据可以修改
    int * const ptr3_num1 = &num1;
    //ptr3_num1 = ptr2_num1;//不合法
    //3.如果有两个const位于*的左右两侧,表示指针和指针所指向的数据都不能修改

}

//2.const修饰函数参数
void ConstDemo2(const int num)
{
    //num = 123;//传递来的参数num在函数体内不可改变,与修饰时的性质一致
}
class Computer{
public:
    Computer(int core){this->m_core = core;}

    void buy(){}

    void buy(int core)
    {

    }

    /**修改电脑的核心频率*/
    void SetCore(int core){this->m_core = core;}
    int GetCore()const{return m_core;}
private:
    int m_core;//cpu主频
};
void ConstDemo3(const Computer & computer)
{
    //const修饰引用时,不能修改引用对象的任何成员 - 好处是可以保护传递参数:不需要一个新的参数副本(copy)
    // passing 'const Computer' as 'this' argument of 'void Computer::buy(int)' discards qualifiers [-fpermissive]|
    //computer.buy(123);//const成员函数不能调用非const成员函数
    //使用const传递对象的引用时,可以起到不copy对象的目的(节省省略)
}

//3.const修饰返回值
//强调:使用const修饰引用类型的一个常见的原因是:提高效率
const Computer & GetMax(const Computer & com1, const Computer & com2){
    if(com1.GetCore()>com2.GetCore())
    {
        return com1;
    }
    return com2;
}
//如果函数要返回局部对象,就应该直接返回这个对象,而不要返回对象的引用
//在可以返回对象,也可以返回引用时,就应该首选引用,因为效率高

//4.cosnt修饰函数 - 说明函数不会修饰成员变量的值
class TestClass{
public:
    int value;
    void ModifyValue()const{
      //  value = 1111;
    }
};



#endif // CONSTDEMO_H_INCLUDED

main.cpp

#include <iostream>
#include "ConstDemo.h"

using namespace std;

int main()
{
    ConstDemo1();
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值