typename场合、默认模板参数、趣味写法分析

005typename的应用场合_用在模板函数类型成员前表示是一个类型

#include<iostream>
#include<cstdlib>
#include<string>
#include<vector>

using namespace std;

//求字符串的长度
template<typename T>
typename T::size_type GetLength(const T&c)
{
	if (c.empty())
	{
		return 0;
	}
	return c.size();
}


int main(void)
{
	string mytest = "jisuanjizuchegnyuanli";
	string::size_type size = GetLength(mytest);//等价于无符号整形

	
	system("pause");
	return 0;
}

/*
*(1)typename的使用场合
*	1.在模板定义里面。typename标明其后的参数是类型参数		template<typename T,int a,int b>
*	template<typename T>		//名字为T的模板参数
*	typename 可以使用class,这里的class不是类定义,表示类型参数
*	2.使用类的类型成员,用typename来表示这是一个类型
*	::可以表示类成员作用域
*	::还可以表示访问模板类的类型成员。
*				函数返回值
*		typename myVector<T>::myIterator   myVector<T>::myend()
*		typename 的用处就是显式告诉编译器myVector<T>::myIterator是一个类型
*
		typename T::size_type GetLength(const T&c)
		中的T::size_type也是类型,所以前面需要加上typename
*
*(2)
*
*(3)
*
*
*/

006函数指针调用函数

#include<iostream>
#include<cstdlib>
#include<string>
#include<vector>


using namespace std;
int myFunction(int a,int b)
{
	return  a + b;
}
//把函数指针作为另外一个函数的参数传递??
//
//定义一个函数指针类型
typedef int(*FunType)(int, int);
//定义一个函数接收函数的指针
void TestFunction(int i,int j,FunType myfun)
{
	//这里可以通过函数指针调用函数
	int result = myfun(i, j);
	cout << result << endl;
}


int main(void)
{
	TestFunction(1, 2, myFunction);//函数名字就相当于一个函数地址
	TestFunction(1, 2, &myFunction);//ok。取地址也可以调用成功

	system("pause");
	return 0;
}

/*
*(1)函数指针做其他函数的参数
*
*
*(2)
*
*(3)
*
*
*/

007函数模板趣味用法_传递一个可调用类对象作为类模板参数

#include<iostream>
#include<cstdlib>
#include<string>
#include<vector>

using namespace std;

int myFunction(int a, int b)
{
	return  a + b;
}


//把函数指针作为另外一个函数的参数传递??
//
//定义一个函数指针类型
typedef int(*FunType)(int, int);

template <typename T,typename F>
void testFunction(const T&i,const T&j,F myfunc)
{
	cout << myfunc(i, j) << endl;
}

class tc
{
public:
	tc()
	{
		cout << "无参构造函数执行" << endl;
	}
	tc(const tc&t)
	{
		cout << "拷贝构造函数执行" << endl;
	}
	//重载()函数调用运算符
	int operator()(int a,int b)
	{
		return a + b;
	}
public:
	
};



int main(void)
{
	testFunction(1, 2,myFunction);//自动推断 T为int,F为函数类型


	tc object01;
	testFunction(3, 4, object01);
	/*
	 * 1.首先调用拷贝构造函数执行,把一个object01拷贝给形参,判断为类 类型
	 * 2.然后调用重载的函数调用()运算符
	 */
	cout << "_______________________________" << endl;
	testFunction(5, 6, tc());//使用一个临时对象,少调用一个拷贝构造函数,只调用了无参构造函数

	
	system("pause");
	return 0;
}

/*
 *传递一个可调用类对象作为类模板参数
 *
*(1)可调用对象概念--具体见“未归类知识点1”
*	1.如果类重载了函数调用运算符(也就是括号运算符),则我们可以像使用函数一样使用该类的对象,
*		我们把这样的类对象称为函数对象(function object)
*	2.C++语言中有几种可调用对象:函数、函数指针、lamba表达式、
*		bind创建的对象以及重载了函数运算符的类。
*
*(2)
*
*(3)
*
*
*/

008默认模板参数

#include<iostream>
#include<cstdlib>
#include<string>
#include<vector>
#include"myArray.h"
using namespace std;

int myFunction(int a, int b)
{
	return  a + b;
}


//把函数指针作为另外一个函数的参数传递??
//
//定义一个函数指针类型
typedef int(*FunType)(int, int);

template <typename T, typename F=FunType>//声明的时候指定类型
void testFunction(const T&i, const T&j, F myfunc=myFunction)//有默认参数
{
	cout << myfunc(i, j) << endl;
}

int main(void)
{
	//a
	myArray<> my_array01;//完全用模板参数的默认缺省值
	myArray<int >my_array02;//第二个参数使用默认缺省值


	//b.函数模板的默认参数 testFunction最后参数有默认值
	testFunction(3, 4);
	
	system("pause");
	return 0;
}

/*
*(1)默认模板参数
*	a.类模板--类模板名字后面必须使用<>来提供额外的信息。<>表示这是一个模板
*	b.函数模板的默认参数
*		老标准只能为类模板提供默认模板参数,c++11新标准可以为函数模板提供默认参数
*(2)
*
*(3)
*
*
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值