C++ 可变参函数模板 可变参类模板 模板模板参数

一、可变参函数模板 

template.h

#pragma once
#include <iostream>
#include <map>

void myfunct2()
{
	std::cout << "终止调用" << std::endl;
}
template <typename T, typename...U>
void myfunct2(const T& firstarg, const U&... otherargs)
{
	std::cout << "收到的参数值是:" << firstarg << std::endl;
	myfunct2(otherargs...);
}

main.cpp

#include <iostream>
#include "template.h"
using namespace std;
 
int main()
{
    myfunct2(10, "abc", 12.7);
}

输出:

收到的参数值是:10
收到的参数值是:abc
收到的参数值是:12.7
终止调用

 

二、可变参类模板

template.h

#pragma once
#include <iostream>
#include <map>

template <typename... Args> class myclasst {};
template <typename First, typename... Others>
class myclasst<First, Others...> :private myclasst<Others...>
{
public:
	myclasst() 
	{
		std::cout << "执行了 this:%p" << this << std::endl;
	}
	myclasst(First parf, Others... paro) : m_i(parf), myclasst<Others...>(paro...)
	{
		std::cout << "---------begin---------------" << this << std::endl;
		std::cout << "执行了 this:%p" << this << std::endl;
		std::cout << "m_i:" << m_i << std::endl;
		std::cout << "---------end---------------" << this << std::endl;
	}
	First m_i;
};

template<>
class myclasst<>
{
public:
	myclasst()
	{
		std::cout << "执行了最上层this:%p" << this << std::endl;
	}
};

main.cpp

#include <iostream>
#include "template.h"
using namespace std;
 
int main()
{
    myclasst<int, float, double> myc(12, 13.4F, 56);
}

输出:

执行了最上层this:%p003BFC30
---------begin---------------003BFC30
执行了 this:%p003BFC30
m_i:56
---------end---------------003BFC30
---------begin---------------003BFC30
执行了 this:%p003BFC30
m_i:13.4
---------end---------------003BFC30
---------begin---------------003BFC30
执行了 this:%p003BFC30
m_i:12
---------end---------------003BFC30

 

三、模板模板参数

template.h

#pragma once
#include <iostream>
#include <map>

template <typename T, 
	template<class> class Container
	//template<typename W> typename Container
	>
class myclass
{
public:
	T m_i;
	Container<T> myc;

	myclass()
	{
		for (int i = 0; i < 10; i++)
		{
			myc.push_back(i);
		}
	}
};

main.cpp

#include <iostream>
#include "template.h"
using namespace std;

template<typename T>
using MYVec = vector<T, allocator<T>>;

int main()
{
    myclass<int, MYVec> myvecObj;
}

Container也是个模板。由于是个参数,所以为模板模板参数。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include<iostream> using namespace std; class student; class teacher { public: int a = 1; student *p; teacher(int a); ~teacher(); }; teacher::teacher(int a) :a(a) { cout << "teacher构造函数调用" << endl; ///p = new student; } teacher::~teacher() { cout << "teacher析构函数调用" << endl; } class student { public: //友元函数可以访问类中的公有和私有成员,不可以访问保护成员 friend void func2(student &s); friend class teacher; int a = 10; student(int x); ~student(); void func3(); private: int b = 20; }; student::student(int x) :a(x) { cout << "student 构造函数调用1" << endl; } student::~student() { cout << "student 析构函数调用!" << endl; } void student::func3() { cout << b << endl; } void func1() { student s1(100); cout << s1.a << endl; s1.func3(); func2(s1); cout << s1.a << endl; } //友元函数,全局函数做友元 //在形中加入const防止传入的参数被改变,不加const则可以改变 void func2( student &s) { //传入普通类对象不可以改变类中的值,只能改变形类的值 /*s.a = 123; cout<<s.a << endl; cout << s.b << endl;*/ ////传入指针可以改变类的值 /*s->a = 1000; s->b = 123; cout << s->a << endl; cout << s->b << endl;*/ ///传入引用也可以改变类中的值 //s.a = 12345; //s.b = 123456; cout << s.a << endl; cout << s.b << endl; } //类做友元 void func3() { student s1(10); cout << s1.a << endl; teacher t1(100); cout << t1.a << endl; cout << t1.p->a<< endl; } int main() { //成员函数做友元 //func1(); //类做友元 func3(); system("pause"); return 0; }
06-08

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值