当前位置: 首页 > news >正文

【cmu15445c++入门】(5)c++中的模板类

一、template模板类

除了模板方法【cmu15445c++入门】(4)c++中的模板方法

模板也可以用来实现类

二、代码

/*** @file templated_classes.cpp* @author Abigale Kim (abigalek)* @brief Tutorial code for templated classes.*/// Includes std::cout (printing).
#include <iostream>// Templates can be also used to implement classes. For instance, here is a
// basic templated class that stores one element of a templated type and
// prints it when the print function is called.//模板也可以用来实现类
template<typename T>
class Foo {public:Foo(T var) : var_(var) {}void print() {std::cout << var_ << std::endl;}private:T var_;
};// You can also pass in multiple type names via templates into classes. 
// For instance, here's another basic templated class that stores two
// elements of a templated type and prints them when the print function
// is called.
// 模板类可以支持多个类型
template<typename T, typename U> 
class Foo2 {public:Foo2(T var1, U var2) : var1_(var1), var2_(var2) {}void print() {std::cout << var1_ << " and " << var2_ << std::endl;}private:T var1_;U var2_;
};// It is also possible to create specialized templated classes, that do
// different things for different types. Take the following contrived example,
// which instantiates a class with a print function that outputs the value of
// the variable stored if it's any other type but float. If the class is 
// instantiated with a float type, it prints out hello float and the variable
// the class stores in its var_ field.//模板类也可以针对特定的类型做特定的处理
template<typename T>
class FooSpecial {public:FooSpecial(T var) : var_(var) {}void print() {std::cout << var_ << std::endl;}private:T var_;
};// Specialized templated class, specialized on the float type.
template<>
class FooSpecial<float> {public:FooSpecial(float var) : var_(var) {}void print() {std::cout << "hello float! " << var_ << std::endl;}private:float var_;
};// Template parameters don't have to be types. They can also be values!
template<int T>
class Bar {public: Bar() {}void print_int() {std::cout << "print int: " << T << std::endl;}
};int main() {// First, let us construct an object from a templated class. The Foo// class template is instantiated with an int template argument. This// would make a's type class Foo<int> instead of Foo. a's print // function works as expected.Foo<int> a(3);std::cout << "Calling print on Foo<int> a(3): ";a.print();// It is also possible for a templated class to interpret the type// of its arguments. Once again, if you're a beginner, think twice// before doing this if you are unsure of the types you are // instantiating your class with.Foo b(3.4f);std::cout << "Calling print on Foo b(3.4f): ";b.print();// Second, we construct an object from a templated class with multiple// type arguments.Foo2<int, float> c(3, 3.2f);std::cout << "Calling print on Foo2<int, float> c(3, 3.2f): ";c.print();// Let's see what happens when we instantiate FooSpecial both with// and without the float type argument. As expected when we call// print from d, it prints the variable and not "hello float".// When we call print from e, which is an instance of the// instantiated FooSpecial<float> class, it prints hello float!FooSpecial<int> d(5);std::cout << "Calling print on FooSpecial<int> d(5): ";d.print();FooSpecial<float> e(4.5);std::cout << "Calling print on FooSpecial<float> e(4.5): ";e.print();// Lastly, let's see what happens when we construct an object from a// templated class with non-type arguments.Bar<150> f;std::cout << "Calling print_int on Bar<150> f: ";f.print_int();// Once again, these are contrived examples, but it is still important// to understand them you'll be seeing code similar to this in the Bustub// codebase, so it's good to understand templated classes in these contexts!// 最后,需要注意的是,以上大多数都是人为的示例,但还是要理解下,可能在海量的代码中看到。return 0;
}

三、运行结果

相关文章:

  • flask flask-sqlalchemy sqlit3
  • 如何顺滑使用华为云编译构建平台?
  • J3-DenseNet实战
  • 并发编程(九)
  • Vue3组件库 -- element plus 树形选择器组件怎样显示已有的树形菜单?
  • VuePress部署到GitHub Pages
  • Spark SQL基础
  • 浅研究下 DHCP 和 chrony
  • BSP视频教程第29期:J1939协议栈CAN总线专题,源码框架,执行流程和应用实战解析,面向车通讯,充电桩,模组通信等(2024-01-08)
  • Python 基础(八):函数
  • 一种DevOpts的实现方式:基于gitlab的CICD(二)
  • ROS OpenCV 图像基本处理函数
  • kylin集群使用nginx反向代理
  • 快速预览图片类PDF报告,PDF转文字并统计词频
  • ORB SLAM2 编译
  • 《Java编程思想》读书笔记-对象导论
  • android 一些 utils
  • CAP理论的例子讲解
  • CSS实用技巧干货
  • IOS评论框不贴底(ios12新bug)
  • java取消线程实例
  • JDK 6和JDK 7中的substring()方法
  • leetcode388. Longest Absolute File Path
  • node-glob通配符
  • Python实现BT种子转化为磁力链接【实战】
  • React+TypeScript入门
  • Spark in action on Kubernetes - Playground搭建与架构浅析
  • Transformer-XL: Unleashing the Potential of Attention Models
  • Yeoman_Bower_Grunt
  • 创建一种深思熟虑的文化
  • 翻译:Hystrix - How To Use
  • 猴子数据域名防封接口降低小说被封的风险
  • 前端临床手札——文件上传
  • 深入 Nginx 之配置篇
  • 手机端车牌号码键盘的vue组件
  • 小程序01:wepy框架整合iview webapp UI
  • 在GitHub多个账号上使用不同的SSH的配置方法
  • 智能合约开发环境搭建及Hello World合约
  • 积累各种好的链接
  • 教程:使用iPhone相机和openCV来完成3D重建(第一部分) ...
  • ​​​​​​​Installing ROS on the Raspberry Pi
  • ###STL(标准模板库)
  • #{} 和 ${}区别
  • (8)Linux使用C语言读取proc/stat等cpu使用数据
  • (C语言)fread与fwrite详解
  • (k8s中)docker netty OOM问题记录
  • (二)【Jmeter】专栏实战项目靶场drupal部署
  • (仿QQ聊天消息列表加载)wp7 listbox 列表项逐一加载的一种实现方式,以及加入渐显动画...
  • (分布式缓存)Redis分片集群
  • (黑马C++)L06 重载与继承
  • (欧拉)openEuler系统添加网卡文件配置流程、(欧拉)openEuler系统手动配置ipv6地址流程、(欧拉)openEuler系统网络管理说明
  • (转)C#开发微信门户及应用(1)--开始使用微信接口
  • ******IT公司面试题汇总+优秀技术博客汇总
  • .axf 转化 .bin文件 的方法
  • .Net Core/.Net6/.Net8 ,启动配置/Program.cs 配置