c++ - Friend declaration in class Template

There are three kinds of friends declaration that may appear within a class template. 

 

A nontemplate friend class or friend function.

 

 

/** - A nontemplate friend class or friend function.
* A nontemplate friend class or friend function. In the following example, the function foo(), the member function bar()
* the member function bar() and the class foobar are friends to all instantiation of the class tepmlate QueueItem .
*  
*  this is a one-to-one relationship and the fiends are determined/fixed right in the declaration (there is only one declaration of the friends)
*/
class Foo {
	void bar();
};

class foobar {}

template <class T>
class QueueItem {
	friend class foobar;
	friend void foo();
	friend void Foo::bar();
};

 

 

It is the one-to-one relationship, and there is only one function . and no matter how many instance of the function template , there is only one declaration. 

 

A Bound friend class template or function template.

 

 

/** - A bound friend class template or function template 
*   this is a one-to-one relationship and the friends are not determined (the friends is created when the class is instantiated).-
*/
template <class Type>
class foobar { 
//
};

template <class Type>
void foo(QueueItem<Type>);

template <class Type>
class Queue {
	void bar();
};

template <class Type>
class QueueItem {
	friend class foobar<Type>;
	friend class foo<Type>(QueueItem<Type>);
	friend class Queue<Type>::bar();
};
 

 

this is also a one-to-one relationship, but for a particular instantiation of the class template, there is one friend for that instantiation.

 

 

An unbound friend class

 

 

 

/** - An unbound friend class template or function template 
*  unbound friend classes are multiple-to-one relationship
*   
*/
template <class Type>
template QueueItem
{
	friend class foobar;
	template <class T>
	friend void foo(QueueItem<T>);
	template <class T>
	friend void Queue<T>::bar();
};
 

There is a one-to-many relationship, where for a particular instantiation of the class template, there could be many function/classes that are the friends of the class template instantion. 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值