C++ 常函数 常对象 const

本文解释了C++中const关键字如何影响成员函数的this指针,强调了const修饰的函数和对象限制修改行为,以及mutable修饰成员变量的特例。通过实例展示了常对象只能调用const成员函数且不能直接修改数据,但可通过指针间接操作。

this 指针的本质: Person * const this;

常函数

用 const 修饰成员函数时,const 修饰 this 指针指向的内存区域,成员函数体内不可以修改本类中的任何普通成员变量。

void show() const
{
}

//const Person const this;//限制了 this 指针

当成员变量类型符前用 mutable 修饰时例外。

mutable int m_A;

常对象

常对象只能调用 const 的成员函数

常对象可访问 const 或非 const 成员属性,不能修改,除非成员用 mutable 修饰。

案列

//const修饰成员函数
class Person{
public:
	Person(){
		this->mAge = 0;
		this->mID = 0;
	}
	//在函数括号后面加上const,修饰成员变量不可修改,除了mutable变量
	void sonmeOperate() const{
		//this->mAge = 200; //mAg e不可修改
		this->mID = 10;
	}
	void ShowPerson(){
		cout << "ID:" << mID << " mAge:" << mAge << endl;
	}
private:
	int mAge;
	mutable int mID;
};

int main(){

	Person person;
	person.sonmeOperate();
	person.ShowPerson();

	system("pause");
	return EXIT_SUCCESS;
}
////const修饰类对象
class Person{
public:
	Person(){
		this->mAge = 0;
		this->mID = 0;
	}
	void ChangePerson() const{
		mAge = 100;
		mID = 100;
	}
	void ShowPerson(){
        this->mAge = 1000;
		cout << "ID:" << this->mID << " Age:" << this->mAge << endl;
	}

public:
	int mAge;
	mutable int mID;
};

void test(){	
	const Person person;
	//1. 可访问数据成员
	cout << "Age:" << person.mAge << endl;
	//person.mAge = 300; //不可修改
	person.mID = 1001; //但是可以修改mutable修饰的成员变量
	
	//2. 只能访问const修饰的函数
	//person.ShowPerson();
	person.ChangePerson();
}

不能直接修改, 但是可以通过指针间接修改。

以下为几道关于 C++ 常函数对象的练习题: ### 练习题 1:基本概念理解 ```cpp #include <iostream> using namespace std; class Rectangle { private: int length; int width; public: Rectangle(int l, int w) : length(l), width(w) {} // 定义一个常函数用于计算矩形面积 int getArea() const; // 定义一个普通函数用于修改矩形的长 void setLength(int l); }; // 实现常函数 int Rectangle::getArea() const { // 尝试在这里修改 length 会发生什么? // length = 10; // 取消注释查看错误 return length * width; } // 实现普通函数 void Rectangle::setLength(int l) { length = l; } int main() { const Rectangle rect1(5, 4); Rectangle rect2(3, 6); // 调用常函数 cout << "rect1 的面积是: " << rect1.getArea() << endl; cout << "rect2 的面积是: " << rect2.getArea() << endl; // 调用普通函数 rect2.setLength(7); cout << "修改后 rect2 的面积是: " << rect2.getArea() << endl; // rect1.setLength(8); // 取消注释查看错误 return 0; } ``` **问题**: 1. 为什么在 `getArea` 常函数里修改 `length` 会出错? 2. 为何对象 `rect1` 不能调用 `setLength` 函数? ### 练习题 2:对象常函数的使用 ```cpp #include <iostream> using namespace std; class Book { private: string title; mutable int borrowCount; public: Book(string t) : title(t), borrowCount(0) {} // 定义一个常函数用于显示书名 void displayTitle() const; // 定义一个常函数用于增加借阅次数 void borrowBook() const; // 定义一个普通函数用于修改书名 void changeTitle(string newTitle); }; // 实现常函数 void Book::displayTitle() const { cout << "书名是: " << title << endl; } // 实现常函数 void Book::borrowBook() const { borrowCount++; cout << "借阅次数增加,当前借阅次数: " << borrowCount << endl; } // 实现普通函数 void Book::changeTitle(string newTitle) { title = newTitle; } int main() { const Book book1("C++ Primer"); Book book2("Effective C++"); // 调用常函数 book1.displayTitle(); book1.borrowBook(); book2.displayTitle(); book2.borrowBook(); // 修改书名 book2.changeTitle("C++ Templates"); book2.displayTitle(); // book1.changeTitle("New Book"); // 取消注释查看错误 return 0; } ``` **问题**: 1. `borrowCount` 被声明为 `mutable` 有什么作用? 2. 为什么对象 `book1` 不能调用 `changeTitle` 函数? ### 练习题 3:常函数与普通函数的重载 ```cpp #include <iostream> using namespace std; class Counter { private: int count; public: Counter(int c) : count(c) {} // 定义一个常函数用于显示计数 void showCount() const; // 定义一个普通函数用于显示计数并增加计数 void showCount(); }; // 实现常函数 void Counter::showCount() const { cout << "常函数: 当前计数是: " << count << endl; } // 实现普通函数 void Counter::showCount() { cout << "普通函数: 当前计数是: " << count << endl; count++; } int main() { const Counter c1(5); Counter c2(10); // 调用常函数 c1.showCount(); // 调用普通函数 c2.showCount(); c2.showCount(); return 0; } ``` **问题**: 1. 对象 `c1` 调用的是哪个 `showCount` 函数,为什么? 2. 普通对象 `c2` 调用 `showCount` 函数时,计数会增加,而对象 `c1` 调用时计数不会增加,原因是什么?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值