`

c++ - Friend declaration in class Template

    博客分类:
  • c++
c++ 
阅读更多

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. 

分享到:
评论

相关推荐

    C++-命名空间namespace

    除了通过完整路径访问命名空间成员,C++还提供了两种简化方式:`using directive`和`using declaration`。 - **using directive**: ``` using namespace &lt;命名空间名称&gt;; ``` 这将导入命名空间中的所有成员,...

    3D-vertex-declaration-system.zip

    3D-vertex-declaration-system.zip,用于管理三维图形顶点结构。2013年为个人游戏引擎撰写。,3D建模使用专门的软件来创建物理对象的数字模型。它是3D计算机图形的一个方面,用于视频游戏,3D打印和VR,以及其他应用...

    c-minus词法分析器

    2. declaration_list -&gt; declaration_list declaration | declaration 3. declaration -&gt; var_declaration | fun_declaration 4. var_declaration -&gt; type_specifier ID; | type_specifier ID [ NUM ]; 5. type_...

    林锐--高质量C++-C编程指南

    一个文件用于保存程序的声明(declaration),称为头文件。另一个文件用于保存程序的实现(implementation),称为定义(definition)文件。 C++/C程序的头文件以“.h”为后缀,C程序的定义文件以“.c”为后缀,C++...

    TI-EU Declaration of Conformity (DoC)U-综合文档

    ### TI-EU Declaration of Conformity (DoC)U-综合文档 #### 一、概述 **TI-EU Declaration of Conformity (DoC)U** 文件是德州仪器(Texas Instruments Incorporated, 简称 TI)为其产品所发布的符合性声明。这份...

    Google C++ Style Guide_英文版.pdf

    - **Class Template Argument Deduction:** Use CTAD (Class Template Argument Deduction) to make template class instantiation more concise. - **Designated Initializers:** Use designated initializers for ...

    c++错误解释(比较常见的错误类型)

    在C++中,如果在比较中使用的常量超出范围,会出现constant out of range in comparison错误。在比较中,需要确保常量在合法的范围内。 Conversion may lose significant digits(转换时会丢失意义的数字) -------...

    Turbo C++ 3.0[DISK]

    - C++ 2.1 support, including the new nested class specifications, and support of C++ 3.0 templates. - Support for pre-compiled headers for substantial time savings during subsequent recompiles. ...

    apt-mirror-api-0.1.jar

    Files contained in apt-mirror-api-0.1.jar: META-INF/MANIFEST.MF META-INF/maven/com.moparisthebest.aptIn16/apt-mirror-api/pom.properties META-INF/maven/...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    For example, if your header file uses the File class in ways that do not require access to the declaration of the File class, your header file can just forward declare class File; instead of having ...

    C++ 标准 ISO 14882-2011

    可重载声明(Overloadable declarations)、声明匹配(Declaration matching)、重载解析(Overload resolution)、重载函数的地址(Address of overloaded function)、重载运算符(Overloaded operators)、内建...

    17-declaration-merging(声明合并17).pdf

    export class MyClass {} } namespace MyModule { export const myConstant = 42; } ``` 合并后的`MyModule`将包含`MyClass`类和`myConstant`常量。命名空间的这种合并使得导入和使用第三方库时更加灵活。 总的...

    Addison.Wesley.C++.by.Dissection.2002.pdf

    Based on the provided information from the file "Addison.Wesley.C++.by.Dissection.2002.pdf," we can derive a comprehensive overview of the C++ programming language as covered in this text. The book is...

    C++出错提示英汉对照表

    Too many type in declaration -----------------说明中类型太多 houjiuming Too much auto memory in function -----------------函数用到的局部存储太多 Too much global data defined in file ----------------...

    Turbo C++ 3.00[DISK]

    - C++ 2.1 support, including the new nested class specifications, and support of C++ 3.0 templates. - Support for pre-compiled headers for substantial time savings during subsequent recompiles. ...

    C BNF grammar

    - **declaration-specifiers:** (storage-class-specifier | type-specifier | type-qualifier)+ - **storage-class-specifier:** ("auto" | "register" | "static" | "extern" | "typedef") - **type-specifier:...

    css-declaration-sorter:快速并自动按特定顺序对CSS声明进行排序

    CSS声明排序器 一个Node.js模块和插件,可根据其属性名称对CSS,SCSS或Less声明进行排序。 确保样式井井有条,更一致且井井有条...该软件包的目标是在构建过程中对项目的源代码进行排序,或者减小分布式CSS压缩后的...

Global site tag (gtag.js) - Google Analytics