C++模板类用作参数传递

前言

在模板类<>传递参数的一种实现。记不住,以此记录。

// dome.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
//#include "tools.h"
#include <functional>
using namespace std;
#include <vector>

template <class T>
class A 
{
public:
	T name;

};
template <class T>
class AA
{
public:
	T name;

};
template <template<class> class TT, class T, class T1> //声明形式
class B 
{
public:
	TT<T> a;
	T1 age;
};

int main()
{
	//传递的是对象A或者AA
	B<A, string, int> b;
	b.a.name = "222";
	cout << b.a.name << endl;

	B<AA, string, int> bb;
	bb.a.name = "222";
	cout << b.a.name << endl;
	return 0;
}

结果:

猜你喜欢

转载自blog.csdn.net/weixin_47723549/article/details/134076425