C++知识点梳理五const成员

本文介绍了C++中的const成员,包括const数据成员和const成员函数,阐述了它们的使用规则及调用注意事项。接着讨论了static成员,包括static数据成员和静态成员函数,强调了它们的类属特性与初始化方式。此外,还讲解了友元函数的概念,如何打破类的权限限制。最后提到了this指针和explicit关键字的作用,帮助理解对象的内部工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

const成员

  • ++引入了新的关键字const
  • const数据成员
    • const类型变量是不可以修改,只读模式
    • 必须采用初始化参数列表方式进行初始化
  • const成员函数
    • 写法上, const写在函数后面
    • 常成员函数是不能够修改数据成员,只读数据成员
    • 常成员函数可以与普通函数同时存在
      • 普通函数和常成员函数相同时,普通对象优先调用普通函数
      • 普通对象可以调用常成员函数
  • const对象,const修饰的对象
    • 常对象只能调用常成员函数
#include<iostream>
#include<string>
using namespace std;
class MM 
{
public:
	MM(string name, int age) :name(name), age(age) {}
	void print() 
	{
		cout << name << "\t我是普通函数\t" << age << endl;
	}
	void print()const//常函数
	{
		cout << name << "\t我是常成员函数\t" << age << endl;
	}
protected:
	string name;
	const int age;//常成员
};
int main() 
{
	MM mm("对象",18);
	mm.print();
	const MM mm2("常对象",19);
	mm2.print();
	return 0;
}

static成员

  • C++引入了新的关键字static
  • static成员是不属于对象,是属于类的,意味着是所有对象共有的,调用可以不需要对象,当然你可以用对象调用
  • static成员依旧受权限限定
    • static数据成员
      • 必须在类外初始化,不再需要static修饰,但是需要类名限定
      • 类中初始化是错误的,不能采用初始化参数列表的方式初始化
    • static成员函数
      • static写在前面即可
      • 调用非静态成员,必须要指定对象
    • static对象释放是最后释放的
#include<iostream>
#include<string>
#define NUM 18
using namespace std;
class MM
{
public:
	MM(string name) :name(name) {}
	void print() 
	{
		cout << name<<" "<< age << endl;
	}
protected:
	string name;
public:
	static int age;
};
int MM::age = NUM;//初始化static成员
int main() 
{
	cout << MM::age << endl;//可以直接使用,不需要对象
	MM mm("小芳");
	mm.age;
	mm.print();
	return 0;
}

友元函数

  • C++引入了新的关键字friend

  • 友元只是提供一个场所,赋予对象具有打破类的权限定(无视权限)

  • 普通友元函数

#include<iostream>
#include<string>
using namespace std;
class MM
{
public:
	MM(string name, int age, double money) :name(name), age(age), money(money) {}
	void print()
	{
		cout << name << "\t" << age << "\t" << money << endl;
	}
	friend void printdata()
	{
		MM mm2("有缘对象",20,13.5);
		cout << mm2.name << "\t" << mm2.age << "\t" << mm2.money << endl;
	}	
protected:
	string name;
	int age;
private:
	double money;	
};
void printdata();
int main() 
{
	MM mm("小芳",18,10.5);
	mm.print();
	printdata();//直接调用友元函数
	return 0;
}
  • 互为友元函数
    * B类
    * A类
    * A类的友元函数(B类的数据成员)
    * B类的友元函数(A类的数据成员)
#include<iostream>
#include<string>
using namespace std;
class MM 
{
public:
	friend class GG;
	void print();
protected:
	string  prin = "A";
};
class GG 
{
public:
	friend class MM;
	void print() 
	{
		MM mm;
		cout << mm.prin << endl;
	}
protected:
	string prin = "B";
};
void MM::print()
{
	GG gg;
	cout << gg.prin << endl;
}
int main() 
{
	MM mm;
	mm.print();
	GG gg;
	gg.print();
	return 0;
}

this指针和explicit

  • C++引入了新的关键字this
  • this指针
    • 避免形参名和数据成员同名
#include<iostream>
#include<string>
using namespace std;
class MM 
{
public:
	MM(string name, int age) :name(name), age(age) {}
	void printdata(string name,int age) 
	{
		this->name = name;
		this->age = age;
	}
	void print() 
	{
		cout << this->name << "\t" << this->age << endl;
	}
protected:
	string name;
	int age;
};
int main() 
{
	MM mm("小芳",18);
	mm.print();
	mm.printdata("小丽",20);
	mm.print();
	return 0;
}
  • 充当函数返回值
#include<iostream>
#include<string>
using namespace std;
class MM 
{
public:
	MM(string name, int age) : name(name), age(age) {}
	MM MMreturn() 
	{
		return *this;//充当对象本身的返回值
	}
	void print() 
	{
		cout << name << "\t" << age << endl;
	}
protected:
	string name;
	int age;
};
int main() 
{
	MM mm("小芳",18);
	mm.MMreturn().MMreturn().MMreturn().print();
	return 0;
}
  • 静态成员函数不能使用this指针
  • C++引入了新的关键字explicit
    • 作用是修饰构造函数使用,不让隐式转换构造
#include<iostream>
using namespace std;
class MM 
{
public:
	//MM(int age) :age(age) {}
	explicit MM(int age) :age(age) {}
	void print() 
	{
		cout << age << endl;
	}
protected:
	int age;
};
int main() 
{
	/*MM mm = 10;		隐式转换
	mm.print();*/
	MM mm(12);
	mm.print();
	return 0;
}

谢谢大家的阅读,新手上路,如有不足之处请及时指出,我好纠正,万分感激~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值