当前位置: 首页 > 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;
}

三、运行结果

http://www.lryc.cn/news/278258.html

相关文章:

  • MongoDB聚合:$bucket
  • 从优化设计到智能制造:生成式AI在可持续性3D打印中的潜力和应用
  • vue3 响应式api中特殊的api
  • 【大厂算法面试冲刺班】day2:合并两个有序链表
  • 【JaveWeb教程】(19) MySQL数据库开发之 MySQL数据库操作-DML 详细代码示例讲解
  • Web前端篇——ElementUI之el-scrollbar + el-backtop + el-timeline实现时间轴触底刷新和一键返回页面顶部
  • CAS-ABA问题编码实战
  • Linux 常用进阶指令
  • windows通过ssh连接Liunx服务器并实现上传下载文件
  • 【K8S 存储卷】K8S的存储卷+PV/PVC
  • 工业智能网关如何保障数据通信安全
  • 基于Springboot的课程答疑系统(有报告)。Javaee项目,springboot项目。
  • 操作系统 内存相关
  • 【模拟IC学习笔记】 PSS和Pnoise仿真
  • IPv6邻居发现协议(NDP)---路由发现
  • OpenPLC v3 代码结构
  • 安全防御之备份恢复技术
  • 条款39:明智而审慎地使用private继承
  • 【数据库原理】(20)查询优化概述
  • FineBI实战项目一(18):每小时上架商品个数分析开发
  • Pytorch常用的函数(六)常见的归一化总结(BatchNorm/LayerNorm/InsNorm/GroupNorm)
  • 业务记录笔记
  • Leetcode16-有多少小于当前数字的数字(1365)
  • JavaWeb- Tomcat
  • Android studio 各本版下载
  • [C#]winform部署PaddleOCRV3推理模型
  • 谈谈Spring Bean
  • kubernetes(一)概述与架构
  • 【Scala】——变量数据类型运算符
  • 嵌入式培训机构四个月实训课程笔记(完整版)-Linux系统编程第十天-Linux下mplayer音乐播放器练习题(物联技术666)