当前位置:网站首页>模板函数与特化函数实现高效dynamicCast
模板函数与特化函数实现高效dynamicCast
2022-06-29 22:09:00 【kupeThinkPoem】
目录
一、概述
模板是一种参数化的多态工具 所谓参数化的多态性,是指将程序所处理 的对象的类型参数化,使一段程序代码可以用于处理多不同类型的对象。 采用模板编程,可以为各种逻辑功能相同而数据类型不同的程序提供一种代码共享的机制。模板包括函数模板(function template)、类模板(class template)。本文主要讨论函数模板
二、模板函数
函数模板为所有的函数提供唯一的一段函数代码,增强了函数设计的通用性。使用函数模板的方法是先说明函数模板,然后实例化成相应的模板函数进行调用执行 函数模板不是函数,不能被执行置换代码中的类型参数得到模板函数——实例化,实例化后的模板函数是真正的函数,可以被执行。
实例化之前,先检查模板代码本身,查看语法是否正确;在这里会发现语法错误,如果遗漏分号等。 实例化期间,检查模板代码,查看是否所有的调用都有效。在这里会发现无效的调用,如该实例化类型不支持某些函数调用或操作符等。普通函数只需要声明,即可顺利编译,而模板的编译需要查看模板的定义(声明和定义需放在同个.h文件)。
三、模板特化
1、全特化
template <class T1,class T2>
void calc(T1 a, T2 a)
{
}
template <>
void calc(double a, double a)
{
}2、局部特化
(1)、局部特化参数
template <class T1,class T2>
void calc(T1 a, T2 b)
{
}
template <>
void calc(T1 a, double b)
{
}(2)、局部特化为指针类型
template <class T1,class T2>
void calc(T1 a, T2 b)
{
}
template <class T1,class T2>
void calc(T1* a, T2 *b)
{
}(3)例子
假设现在我们有这样一个模板函数max:
template <typename T> const T& max(const T& a, const T& b) {
return a < b ? b : a;
}然后现在我们要比较两个字符串的大小,如:
const char* str1 = "ttt"; const char* str2 = "ccc";此时如果按一般的实例化,比较的将是str1 和 str2 的大小,即比较指针数值大小,而不是字符串大小,故我们需要实现一个模板函数的特化,如下:
template<> const char* const& max(const char* const& a, const char* const& b) {
return strcmp(a, b) < 0 ? b : a;
}四、dynamicCast的模板实现
class A
{
public:
virtual ~A(){}
virtual int getType()
{
return 0;
}
};
class B:public A
{
public:
virtual int getType()
{
return 1;
}
};
template <class Target, class Source>
inline Target* dynamicCast(Source* x)
{
Target tmp = dynamic_cast<Target>(x);
return tmp;
}
template<>
inline B* dynamicCast(A* x)
{
if(x->getType()==1)
{
return (B*)(x);
}
return NULL;
}
int _tmain(int argc, _TCHAR* argv[])
{
A* a=new B();
B*b=dynamicCast<B>(a);
delete a;
return 0;
}
五、参考
边栏推荐
- #第三天
- ASP.NET 跨页面提交(Button控件页面重定向)
- Grep工具
- If I am in Zhuhai, where can I open an account? Is it safe to open an account online?
- Graduation summary of construction practice camp
- Hidden worries behind the listing of shushulang: the performance has declined significantly, the market position is relatively backward, and the competitiveness is questionable
- Introduction to gaofen-3 satellite (GF-3)
- 22 years of a doctor in Huawei
- With the rise of China's database, Alibaba cloud lifeifei: China's cloud database has taken the lead in various mainstream technological innovations abroad
- Portable 4K audio and video conference terminal all-in-one machine with 8x digital zoom
猜你喜欢

ASP. Net cross page submission (button control page redirection)

联通入库|需要各地联通公司销售其产品的都需要先入总库

2022 openvino DevCon unveils secrets! Intel and many partners deepen the construction of developer ecology and release the innovation potential of AI industry

Guangzhou launched a campaign to promote the safety of bottled gas and popularized the knowledge of gas safety

Reading notes on how to connect the network - LAN on the server side (4)

What are the software testing methods and technical knowledge points?

The soft youth under the blessing of devcloud makes education "smart" in the cloud

一键式文件共享软件Jirafeau

ASP using panel to realize simple registration page

This time, I will talk about technology and life
随机推荐
从第三次技术革命看企业应用三大开发趋势
掌握这28张图,面试再也不怕被问TCP知识了
Motianlun "high availability architecture" dry goods document sharing (including 124 Oracle, MySQL and PG materials)
为什么在局域网(ERP服务器)共享文件夹上拷贝文件时导致全局域英特网断网
一键式文件共享软件Jirafeau
Can the flick CDC be used for incremental synchronization from Oracle to MySQL
IFLYTEK AI learning machine summer new product launch AI + education depth combination to create a new height of products
证券开户选择哪个证券另外想问,现在在线开户安全么?
【Proteus仿真】步进电机转速数码管显示
MooseFS的简介、部署及应用
DevCloud加持下的青软,让教育“智”上云端
论文浅尝 | KR-GCN: 知识感知推理的可解释推荐系统
Grep工具
How to use filters in jfinal to monitor Druid for SQL execution?
Is it reliable to open an account on the compass with your mobile phone? Is there any hidden danger in this way
软件快速交付真的需要以安全为代价吗?
Data mining review
新手必须知道的 Kubernetes 架构
直播平台开发,进入可视区域执行动画、动效、添加样式类名
这个flink cdc可以用在做oracle到mysql的,增量同步吗