C++const关键字作用

修饰普通变量,表示不可修改(在定义的时候必须初始化)

#include <iostream>
using namespace std;
const int a1 = 10;
int main()
{
	const int a3; // 错误,没有初始化
    const int a2 = 10;
    a1 = 10;    // 错误,不可修改变量的值
    int b1 = a1; // 可以给其余变量赋值
}

修饰指针

const修饰指针时分3种情况:常量指针、指针常量、常指针常量。

常量指针

#include <iostream>
using namespace std;
int main()
{
    int a = 10;
    // 常量指针,指针指向对象的值不能变,指针指向的对象可以变
    const int* ptr = &a;
    *ptr = 100;  //错误
    int b = 100;
    ptr = &b; // 正确
}

指针常量

#include <iostream>
using namespace std;
int main()
{
    int a = 10;
    // 指针常量,指针指向的对象不可以变,指向对象的值可以变
    int* const ptr = &a;
    *ptr = 100; // 正确
    int b = 100;
    ptr = &b; // 错误
}

常指针常量

#include <iostream>
using namespace std;
int main()
{
    int a = 10;
    // 常指针常量,指针指向的对象、指向对象的值都不能变
    const int* const ptr = &a;
    *ptr = 100; // 正确
    int b = 100;
    ptr = &b; // 错误
}

const用于函数中

修饰函数的形参

表示函数中不能改变该形参的值。

#include <iostream>
using namespace std;
void function(const int a)
{
    cout<<a<<endl;
    a++;// 错误,a用const修饰,大小不能改变
}
int main()
{
    int a = 10;
    function(a);
}

修饰函数中的指针形参

防止意外修改指针指向对象的值。

#include <iostream>
using namespace std;
void function(const int* a,int* const b)
{
    int temp = 100;
    *a = 10;    // 错误
    a = &temp;  // 正确
    b = &temp;  // 错误
    *b = 20;    // 正确
}
int main()
{
    int a = 10;
    int b = 10;
    function(&a,&b);
}

修饰函数返回值

当函数返回值为int&类型时,可以作为左值
当函数返回值为const int&类型时,不可以作为左值

#include <iostream>
using namespace std;

int& test01(int& a)
{
	return a;
}

const int& test02(int& a)
{
	return a;
}
int main()
{
	// 函数返回值为int&类型时,可以作为左值进行赋值
	int a = 10;
	test01(a) = 100;
	cout << a << endl;

	// 函数返回值为const int&类型时,不可以作为左值进行赋值
	int b = 10;
	test02(b) = 100;

}

const用于类中

修饰成员函数(常函数)

const修饰成员函数时,表示成员函数中成员变量不能修改。

#include <iostream>
using namespace std;
class test
{
public:
    test(int value):m_value(value){}
    test(){}
    ~test(){}
    void print_value() const
    {
        //this->m_value = 100; // 错误,const修饰成员函数中,不能修改成员变量的值
        cout<<this->m_value<<endl;
    }
    const int get_value()
    {
        return this->m_value;
    }
private:
    int m_value;
};

int main()
{
    test Test(20);
    int value = Test.get_value();
    cout<<value<<endl;
}

如果想让const成员变量可以在成员函数中修改的话,可以加一个关键字mutable。

#include <iostream>
using namespace std;
class test
{
public:
    test(int value):m_value(value){}
    test(){}
    ~test(){}
    void print_value() const
    {
        this->m_value = 100; // 此时可以,因为成员变量被mutable修饰
        cout<<this->m_value<<endl;
    }
    const int get_value()
    {
        return this->m_value;
    }
private:
    mutable int m_value;
};

int main()
{
    test Test(20);
    int value = Test.get_value();
    Test.print_value();
    cout<<value<<endl;
}

修饰类对象(常对象)

常对象只能调用常成员函数。不能修改成员变量的值(除非有mutable修饰)

#include <iostream>
using namespace std;
class test
{
public:
	test(int value) :m_value(value) {}
	test() {}
	~test() {}
	void print_value() const
	{
		cout << this->m_value << endl;
	}
	int get_value()
	{
		return this->m_value;
	}
	int m_value;
	mutable int m_date;
};

int main()
{
	const test test01(100);
	// 错误,不能修改常对象中的成员变量的值,除非加mutable
	test01.m_value = 1000;
	// 可以,因为m_date类型为mutable int
	test01.m_date = 2021;
	// 可以,常对象可以调用常函数
	test01.print_value();
	// 不可以,常对象只能调用常函数
	int val = test01.get_value();
}

参考文章:
https://www.runoob.com/w3cnote/cpp-const-keyword.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值