0
点赞
收藏
分享

微信扫一扫

C++进阶第十篇

c一段旅程c 2022-03-14 阅读 48

函数模板

1. T  和  a 完全一致

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

template<typename T>

void fun(T a)
{
	T x, y;
	cout << typeid(T).name() << "   ";
	cout << typeid(a).name() << endl;
}

int main()
{
	int x = 10;
	const int y = 20;
	int *xp = &x;
	const int *yp = &y;
	
	fun(x);      // T ==> int              a ==> int  
	fun(y);      // T ==> const int        a ==> const int
	fun(xp);     // T ==> int*             a ==> int*
	fun(yp);     // T ==> const int*       a ==> const int*

	system("pause");
	return 0;
}

2.



template<typename T>

void fun(T a)
{
	T x, y;
	cout <
举报

相关推荐

0 条评论