模板类和非模板类的友元函数

友元函数是C++中的一种特性,允许非成员函数访问类的私有和保护成员。它们在类中声明,类外定义。文章提供了普通类和模板类友元函数的示例,并强调了友元函数声明并不等同于函数定义,需要在外部再次声明。此外,还讨论了显式实例化和名称空间对友元函数的影响。
摘要由CSDN通过智能技术生成

友元函数是指某些虽然不是类成员却能够访问类的所有成员的函数。友元函数在类中声明,在类外定义,使用时不依赖于类或类实例。友元函数的函数可以来自于其他类的成员函数,也可以来自全局函数。

普通类的友元函数

一个简单的友元函数的例子如下:

//t1.h
#include <iostream>
using namespace std;

class My {
private:
	int my;
public:
	My()=default;
	My(int my):my(my) {};
	void printm();
	friend void printfri();
	friend void printfri2(My&);
};
void printfri();//见attention1
void printfri2(My&);//见attention1


//t1.cpp
#include "t1.h"

void printfri() {
    cout << "My print friend." << endl;
}

void printfri2(My& m) {
    cout << "My print friend2: " << m.my << endl;
}

void My<T>::printm() {
	cout << "My print: " << my << endl;
}


//t2.cpp
#include "t1.h"
int main() {
	My m(3);
	m.printm();
	printfri();
	printfri2(m);
	return 0;
}
/*
*/

模板类的友元函数

对于模板类,情况会稍复杂一些。如果友元函数是非模板函数,则直接按照非模板友元函数的声明定义方法去定义;如果友元函数是模板函数,则可以划分为约束友元函数和非约束友元函数,约束友元函数是指对于类的每一个具体化(每一个模板类)都获得与之匹配的具体化的友元,非约束友元函数是指每个函数模板的具体化都是每个类模板具体化的友元。下面的例子是非约束模板友元函数的例子。

//t1.h
#include <iostream>
using namespace std;

template<class T>
class My {
private:
	T my;
public:
	My()=default;
	My(T my):my(my) {};
	void printm();
	friend void printfri();
    template<class K>
	friend void printfri2(My<K>&);
};
void printfri();//见attention1
template <class T> void printfri2(My<T>&);//见attention1


//t1.cpp
#include "t1.h"

void printfri() {
    cout << "My print friend." << endl;
}

template<class T>
void printfri2(My<T>& m) {
    cout << "My print friend2: " << m.my << endl;
}

template<class T>
void My<T>::printm() {
	cout << "My print: " << my << endl;
}
template class My<int>;//显式实例化
template class My<float>;//显式实例化
template void printfri2<int>(My<int>&);//显式实例化
template void printfri2<float>(My<float>&);//显式实例化

//t2.cpp
#include "t1.h"
int main() {
	My<int> m(3);
	m.printm();
	printfri();
	printfri2(m);
	return 0;
}
/*
*/

需要注意的几点

  • attention1

    在类的定义中进行友元的声明仅仅指定了访问的权限,而非一个通常意义上的函数声明。如果我们要调用友元函数,那么我们就必须在友元声明之外再对函数进行一次声明。
    为了使友元函数对类的用户可见,我们通常将友元的函数声明与类放在同一个头文件中。
    虽然有些编译器并未强制限定友元函数必须在使用之前在类的外部进行声明,但是我们最好这样做,因为如果更换了一个有如此强制要求的编译器我们也无须对代码进行改动。
    当类的声明被包含在名称空间块内时,在名称空间内需要声明该友元函数。

参考

模板类的友元函数

友元的声明

c++类的友元函数一定要有形参?

C++ 名称空间与友元函数

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值