C++常函数和常对象学习笔记

一、友元的概述

 类的主要特点之一是数据隐藏,也就是类的私有成员没办法在类的外部访问,但是有时候需要在类的外部访问类的私有成员

 解决方法是使用友元函数,C++允许友元函数访问类的私有成员程序员可以把一个全局函数、某一个类中的成员函数、甚至整个类声明为友元

二、友元的语法

  • friend关键字只出现在声明处
  • 其他类、类成员函数、全局函数都可以声明为友元
  • 友元函数不是类的成员,没有this指针
  • 友元函数可以访问对象任意成员属性,包括私有属性

全局友元函数示例:


#include<iostream>
using namespace std;

class Maker {


	// 声明这个全局函数为该类的友元函数  但是不作为该类的成员函数
	friend void Good(Maker& m);

public:
	Maker()
	{
		keting = "客厅";
		woshi = "卧室";
	}


public:
	string keting;

private:
	string woshi;
};

void Good(Maker& m)
{
	cout << "好朋友访问" << m.keting << endl;
	cout << "好朋友访问" << m.woshi << endl;
}

void test01()
{
	Maker m;
	Good(m);
}

int main()
{
	test01();
	return EXIT_SUCCESS;
}

三、友元类

3.1 通过传入参数来访问类的私有成员

如果A类需要调用B类的私有成员,那么就需要在B类将A类声明为他的友元类:friend class A

#include<iostream>
using namespace std;

class Maker {


	// 声明这个全局函数为该类的友元函数  但是不作为该类的成员函数
	friend void Good(Maker& m);
	friend class GoodF;// 声明GoodF是友元类

public:
	Maker()
	{
		keting = "客厅";
		woshi = "卧室";
	}


public:
	string keting;

private:
	string woshi;
};

class GoodF {

public:
	void func(Maker& bd)
	{
		cout << "访问:" << bd.keting << endl;
		cout << "访问:" << bd.woshi << endl;// 访问类的私有成员
	}

};

void Good(Maker& m)
{
	cout << "好朋友访问" << m.keting << endl;
	cout << "好朋友访问" << m.woshi << endl;
}

void test01()
{
	Maker m;
	Good(m);
}

void test02()
{
	Maker m;
	GoodF f;
	f.func(m);
}

int main()
{
	test02();
	return EXIT_SUCCESS;
}

3.2 通过类内指针来访问类的私有成员

#include<iostream>
#include<string>
using namespace std;

class Maker {

	friend class GoodF2;// 声明为友元类

public:
	Maker()
	{
		keting = "客厅";
		woshi = "卧室";
	}


public:
	string keting;

private:
	string woshi;
};

class GoodF2 {
public:
	GoodF2()
	{
		p = new Maker;// 申请堆区空间
	}

	// 通过对象指针 访问私有成员
	void func()
	{
		cout << "访问:" << p->keting << endl;
		cout << "访问:" << p->woshi << endl;// 访问类的私有成员
	}

	// 拷贝构造  防止浅复制
	GoodF2(const GoodF2& f2)
	{
		// 申请空间
        p = new Maker;// 已经初始化
	}

	~GoodF2()
	{
		if (p != NULL)
		{
			// 释放空间
			delete p;
		}
	}

public:
	Maker* p;

};
void test02()
{
	GoodF2 f;
	f.func();

	GoodF2 f2 = f;// 调用拷贝构造
}

int main()
{
	test02();
	return EXIT_SUCCESS;
}

四、类的友元成员函数

类的成员函数成为另一个类的友元函数,不建议,直接将该类声明为另一个类的友元

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include<string>

// 编译器知道类的声明  不知道类的结构

class Building;// 声明Building 

class Building
{
	// 声明该类为Building 的友元类
	//friend class GoodF;

	// 声明GoodGay类的成员函数func成为Building类的友元函数
	friend void GoodF::func(Building &bud);

public:
	Building()
	{
		keting = "客厅";
		woshi = "卧室";
	}

public:
	string keting;

private:
	string woshi;
};


class GoodF
{
public:
	void func(Building& bd);
	
};

// 实现 加上作用域
void GoodF::func(Building& bd)
{
	cout << bd.keting << endl;
	cout << bd.woshi << endl;// 会报错
}

void test01()
{
	Building b;
	GoodF f;
	f.func(b);
}


int main()
{

	test01();

	return EXIT_SUCCESS;
}

五、友元类的注意

  • 友元关系不能被继承
  • 友元关系是单向的,类A是类B的朋友,但是类B不一定是类A的朋友
  • 友元关系不具有传递性,类B是类A的朋友,类C是类B的朋友,但是类C不一定是类A的朋友
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

少写代码少看论文多多睡觉

求打赏,求关注,求点赞

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值