c++类模板中的友元声明及模板构造函数
类模板的友元声明:
当授予给定模板的所有实例的访问权的时候,在作用域中不需要存在该类模板或函数模板的声明。想要限制对特定实例化的友元关系时,必须在可以用于友元声明之前声明类或函数。
template <class T> class test { template <class U> friend ostream& operator<< (ostream &os, const test<U> &obj); //友元的所有实例均具有访问权 ... }; class test; template <class Type> ostream& operator<< (ostream &os, const test<Type> &obj); template <class T> class test { friend ostream& operator<< <T> (ostream &os, const test<T> &obj);//友元为的T类型实例才有访问权 ... };
登录后复制
模板构造函数:
在一个模板类中,构造函数和模板构造函数同时存在时,优先调用构造函数。只有当确切符合模板构造函数的接口时,才调用模板构造函数。编译器永远不会把模板构造函数视为构造函数,即使客户没有自己定义拷贝构造函数,编译器也会生成一个默认的拷贝构造函数。
template <class T> class test { public: test() { cout << "in my test construct" << endl;} test(const test &) { cout << "in my test copy" << endl;} template <class V> test(const test<V> &) { cout << "in my template copy" << endl;} }; int main() { test<int> t1; test<int> t2(t1); test<double> t3(t1); return 0; }
登录后复制
此处的 template
程序的输出结构为:
in my test construct
in my test copy
in my template copy
在stl的pair和auto_ptr类中有应用
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章
刺客信条阴影:贝壳谜语解决方案
1 个月前
By DDD
Windows 11 KB5054979中的新功能以及如何解决更新问题
3 周前
By DDD
在哪里可以找到原子中的起重机控制钥匙卡
1 个月前
By DDD
如何修复KB5055523无法在Windows 11中安装?
2 周前
By DDD
Inzoi:如何申请学校和大学
3 周前
By DDD

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)