(C++学习笔记六)类的空间长度;this 指针概念;空指针访问成员函数;const 修饰成员函数 ;友元

本文介绍了C++中的类成员变量与成员函数的存储、this指针的使用、空指针调用成员函数的注意事项、const修饰成员函数的作用以及友元的概念和实现方式。通过示例代码展示了各种情况下的用法,并提供了运行结果。

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

八.类的空间长度;this 指针概念;空指针访问成员函数;const 修饰成员函数 ;友元

1.Code :

#include<iostream>
using namespace std;


//  一.  类内 成员变量 与 成员函数 分开存储 
//只有非静态成员变量才属于类的对象上
//简言之,只有非静态成员变量才占类的空间长度

class Person
{
public:
	short a;//非静态成员函数    占 对 象 空 间

	static int b;//静态成员变量不占对象空间

	Person()
	{
		a = 0;
	}

	void func()//函数不占对象空间,所有函数共享一个函数实例
	{
		cout << "a = " << this->a << endl;
	}

	static void func1()//静态函数也不占对象空间 
	{
		cout << "b = " << b << endl;
	}
};


int Person::b = 0;


//  二. this 指针概念

//1.this 指针指向被调用的成员函数所属的对象
//  this 指针是隐含每一个非静态成员函数内的一种指针
//  this 指针无需定义,直接使用即可
//2.用途: (1) 当形参与成员变量同名时,可用 this 指针来区分.
//        (2) 在类的非静态成员函数中返回对象本身,可使用 return *this;

class Person1
{
public:
	Person1(int age)
	{
		this->age = age;//当形参与成员变量同名时,可用 this 指针来区分.
	}

	Person1& PersonAdd(Person1 P)
	{

		this->age += P.age;

		return *this;// 返回 Person1 对象本身
	}
	int age;
};

void test()
{
	Person1 P1(10);//创建新对象 并调用构造函数 Person1(int age);
	cout << "P1.age = " << P1.age << endl;

	Person1 P2(10);
	P2.PersonAdd(P1).PersonAdd(P1).PersonAdd(P1).PersonAdd(P1);
	//10+   10      +     10      +      10     +    10       = 50;            
	cout << "P2.age = " << P2.age << endl;
}

//  三.空指针访问(调用)成员函数
// 使用时需注意 有无用到this指针 ,若用到,需加以判断保证代码的 健壮性?

class Person2 
{
public:

	void show()
	{
		cout << " 函数 show " << endl;
	}

	void show_NULL()
	{
	    if (this == NULL) { return; }
		//若此句注释掉则,下面的语句就会有以下 注释 的原因提示错误

		cout << a << endl;
		//引发了异常: 读取访问权限冲突。
	    //	** this** 是 nullptr。
	}

public:
	int a;
};

void test1()
{
	Person2* P3 = NULL;
	P3->show();         //空指针,可以调用成员函数
	P3->show_NULL();    //但若成员函数中用到了 this 指针,就无法调用了.
}

//  四. const 修饰成员函数 
//常函数:
//    1. 成员函数后加const 后,我们称其为常函数
//    2. 常函数内不可修改成员属性
//    3. 成员属性声明时加关键字 mutable 后,在常函数中依然可以修改
//常对象:
//    1. 声明对象前加 const 称该对象为常对象
//    2. 常对象只能调用常函数

class Person3
{
public:
	Person3()
	{
		a = 0;
		b = 0;
	}

	void func() const//         常函数
	{
		this->b = 100;

		cout << "常函数" << endl;
		cout << "b = " << b <<endl;

		//a = 1000;//error 

	    // const int* const pointer = &a;
		//this = NULL;//error 不能修改指针的指向 Person3* const this;

	}

	void func1() 
	{
		cout << "正常函数" << endl;
	}

public:
	int a;//在常函数中不可修改
	mutable int b;//在常函数中可修改的
};


void test2()
{
	const Person3 Per1;//         常对象

	cout << "Per1.a = " << Per1.a << endl;
	//常对象 可以访问成员变量的值
	//Per1.a = 147;
	//error 常对象 可以访问成员变量的值,但是不能修改
	//(mutable 修饰的变量除外)

	Per1.b = 162;//mutable 修饰的变量 在常函数中可修改

	cout << "Per1.b = " << Per1.b << endl;

	//Per1.func1();//error 常对象只能调用常函数
	Per1.func();//常对象只能调用常函数
}


//   五. 友元
// 1. 关键字: friend
// 2. 作用  : 允许一个函数/类 访问另一个类中私有成员
// 3. 三种实现方式:
//                (1) 全局函数做友元
//                (2) 类做友元
//                (3) 成员函数做友元   例:class sdf;
// 成员函数做友元时一定要注意语句的顺序

class Myroom;//一定得加,否则错误

class sdf
{
public:
	sdf();//构造函数
	

	void sdf_look();
	
private:
	Myroom* myroo;
};

class Myroom
{

	friend void friend1(Myroom * M_copy); 
	//设定函数 friend1 可访问类中私有功能

	friend class F_L;

	friend void sdf::sdf_look();

public:
	int S = 1234;
private:
	int D = 4321;

};

//                (1) 全局函数做友元

void friend1(Myroom* M_copy)//形参为 Myroom类 的地址
{
	M_copy->S = 1235;
	M_copy->D = 4320;

	cout << "friend1 访问(公共)  M_copy->S =  " << M_copy->S << endl;
	cout << "friend1 访问(私有)  M_copy->D =  " << M_copy->D << endl;
}

//                (2) 类做友元

class F_L 
{
public:
	F_L()//构造函数
	{
		myroom = new Myroom;//在堆区创建新内存
	}

	void look()
	{
		myroom->S = 1236;
		myroom->D = 4321;

		cout << "friend2 访问(公共)  myroom->S =  " << myroom->S << endl;
		cout << "friend2 访问(私有)  myroom->D =  " << myroom->D << endl;
	}
private:
	Myroom* myroom;
};

//                (3) 成员函数做友元

sdf::sdf()
{
	myroo = new Myroom;//在堆区创建新内存
}

void sdf::sdf_look()
{
	myroo->S = 17;
	myroo->D = 42;

	cout << "friend3 访问(公共)  myroom->S =  " << myroo->S << endl;
	cout << "friend3 访问(私有)  myroom->D =  " << myroo->D << endl;
}


void test3()
{
	Myroom Myroom1;
	friend1(&Myroom1);//取地址

	F_L friend_lei;
	friend_lei.look();

	sdf friend_han;
	friend_han.sdf_look();
}

int main()
{
	cout << endl << endl <<  " 一. 类内 成员变量 与 成员函数 分开存储 " << endl << endl;
	cout << " 只有非静态成员变量才占类的空间长度 " << endl;
	cout << "sizeof(Person) = " << sizeof(Person) << endl;
	//Person 类中,只有 short a; 这个非静态成员函数占对象(类)空间,因此,长度为 
	// sizeof(short) ,同理,若为int a;,sizeof(Person) = sizeof(int);

	cout << endl << endl << " 二. this 指针概念 " << endl << endl;

	test();

	cout << endl << endl << " 三.空指针访问(调用)成员函数 " << endl << endl;

	test1();

	cout << endl << endl << " 四. const 修饰成员函数 " << endl << endl;

	test2();

	cout << endl << endl << " 五. 友元" << endl << endl;

	test3();



	system("pause");
	return 0;
}

2.运行结果 :

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值