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

三、运行结果

相关文章:

【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 <io…...

MongoDB聚合:$bucket

$bucket将输入文档按照指定的表达式和边界进行分组&#xff0c;每个分组为一个文档&#xff0c;称为“桶”&#xff0c;每个桶都有一个唯一的_id&#xff0c;其值为文件桶的下线。每个桶中至少要包含一个输入文档&#xff0c;也就是没有空桶。 使用 语法 {$bucket: {groupBy…...

从优化设计到智能制造:生成式AI在可持续性3D打印中的潜力和应用

可持续性是现代工业中一个紧迫的问题&#xff0c;包括 3D 打印领域。为了满足环保制造实践日益增长的需求&#xff0c;3D 打印已成为一种有前景的解决方案。然而&#xff0c;要使 3D 打印更具可持续性&#xff0c;还存在一些需要解决的挑战。生成式人工智能作为一股强大的力量&…...

vue3 响应式api中特殊的api

系列文章目录 TypeScript 从入门到进阶专栏 文章目录 系列文章目录一、shallowRef()二、triggerRef()三、customRef()四、shallowReactive()五、shallowReadonly()六、toRaw()七、markRaw()八、effectScope()九、getCurrentScope() 一、shallowRef() shallowRef()是一个新的响…...

【大厂算法面试冲刺班】day2:合并两个有序链表

将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 递归 class Solution {public ListNode mergeTwoLists(ListNode l1, ListNode l2) {if (l1 null) {return l2;}else if (l2 null) {return l1;}else if (l1.val < l2.…...

【JaveWeb教程】(19) MySQL数据库开发之 MySQL数据库操作-DML 详细代码示例讲解

目录 3. 数据库操作-DML3.1 增加(insert)3.2 修改(update)3.3 删除(delete)3.4 总结 3. 数据库操作-DML DML英文全称是Data Manipulation Language(数据操作语言)&#xff0c;用来对数据库中表的数据记录进行增、删、改操作。 添加数据&#xff08;INSERT&#xff09;修改数据…...

Web前端篇——ElementUI之el-scrollbar + el-backtop + el-timeline实现时间轴触底刷新和一键返回页面顶部

ElementUI之el-scrollbar el-backtop el-timeline实现时间轴触底刷新和一键返回页面顶部。 背景&#xff1a;ElementUI的版本&#xff08;vue.global.js 3.2.36&#xff0c; index.css 2.4.4&#xff0c; index.full.js 2.4.4&#xff09; 废话不多说&#xff0c;先看动…...

CAS-ABA问题编码实战

多线程情况下演示AtomicStampedReference解决ABA问题 package com.nanjing.gulimall.zhouyimo.test;import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicStampedReference;/*** @author zho…...

Linux 常用进阶指令

我是南城余&#xff01;阿里云开发者平台专家博士证书获得者&#xff01; 欢迎关注我的博客&#xff01;一同成长&#xff01; 一名从事运维开发的worker&#xff0c;记录分享学习。 专注于AI&#xff0c;运维开发&#xff0c;windows Linux 系统领域的分享&#xff01; 其他…...

windows通过ssh连接Liunx服务器并实现上传下载文件

连接ssh 输入&#xff1a;ssh空格用户名ip地址&#xff0c;然后按Enter 有可能出现下图提示&#xff0c;输入yes 回车即可 输入 password &#xff0c;注意密码是不显示的&#xff0c;输入完&#xff0c;再按回车就行了 以上是端口默认22情况下ssh连接&#xff0c;有些公司它…...

【K8S 存储卷】K8S的存储卷+PV/PVC

目录 一、K8S的存储卷 1、概念&#xff1a; 2、挂载的方式&#xff1a; 2.1、emptyDir&#xff1a; 2.2、hostPath&#xff1a; 2.3、NFS共享存储&#xff1a; 二、PV和PVC&#xff1a; 1、概念 2、请求方式 3、静态请求流程图&#xff1a; 4、PV和PVC的生命周期 5、…...

工业智能网关如何保障数据通信安全

工业智能网关是组成工业物联网的重要设备&#xff0c;不仅可以起到数据交换、通信、边缘计算的功能&#xff0c;还可以发挥数据安全保障功能&#xff0c;保障工业物联网稳定、可持续。本篇就为大家简单介绍一下工业智能网关增强和确保数据通信安全的几种措施&#xff1a; 1、软…...

基于Springboot的课程答疑系统(有报告)。Javaee项目,springboot项目。

演示视频&#xff1a; 基于Springboot的课程答疑系统&#xff08;有报告&#xff09;。Javaee项目&#xff0c;springboot项目。 项目介绍&#xff1a; 采用M&#xff08;model&#xff09;V&#xff08;view&#xff09;C&#xff08;controller&#xff09;三层体系结构&…...

操作系统 内存相关

0 内存 cpu和内存的关系 内存覆盖 内存的覆盖是一种在程序运行时将部分程序和数据分为固定区和覆盖区的技术。这种技术的主要目的是为了解决程序较大&#xff0c;无法一次性装入内存导致无法运行的问题。 具体来说&#xff0c;内存的覆盖技术将用户空间划分为以下两个部分&…...

【模拟IC学习笔记】 PSS和Pnoise仿真

目录 PSS Engine Beat frequency Number of harmonics Accuracy Defaults Run tranisent?的3种设置 Pnoise type noise Timeaverage sampled(jitter) Edge Crossing Edge Delay Sampled Phase sample Ratio 离散时间网络(开关电容电路)的噪声仿真方法 PSS PSS…...

IPv6邻居发现协议(NDP)---路由发现

IPv6路由发现(前缀公告) 邻居发现 邻居发现协议NDP(Neighbor Discovery Protocol)是IPv6协议体系中一个重要的基础协议。邻居发现协议替代了IPv4的ARP(Address Resolution Protocol)和ICMP路由器发现(Router Discovery),它定义了使用ICMPv6报文实现地址解析,跟踪邻…...

OpenPLC v3 代码结构

OpenPLC v3 是一个基于 C 的开源实时自动化平台&#xff0c;主要用于控制和自动化行业中的设备。该项目具有以下主要模块&#xff1a; 1. Core&#xff1a;核心模块&#xff0c;提供数据结构和算法实现。 2. Master&#xff1a;主设备模块&#xff0c;实现与从设备通信的接口。…...

安全防御之备份恢复技术

随着计算机和网络的不断普及&#xff0c;人们更多的通过网络来传递大量信息。在网络环境下&#xff0c;还有各种各样的病毒感染、系统故障、线路故障等&#xff0c;使得数据信息的安全无法得到保障。由于安全风险的动态性&#xff0c;安全不是绝对的&#xff0c;信息系统不可能…...

条款39:明智而审慎地使用private继承

1.前言 在之前挑款32曾讨论了C如何将public继承视为is-a关系&#xff0c;在那个例子中我们有个继承体系&#xff0c;其中class Student以public形式继承class Person&#xff0c;于是编译器在必要时刻将Student转换为Persons。。现在&#xff0c;我在以原先那个例子&#xff0…...

【数据库原理】(20)查询优化概述

查询优化是关系数据库系统设计和实现中的核心部分&#xff0c;对提高数据库性能、减少资源消耗、提升用户体验有着重要影响。虽然挑战重重&#xff0c;但凭借坚实的理论基础和先进的技术手段&#xff0c;关系数据库在查询优化方面有着广阔的发展空间。 一.查询中遇到的问题 数…...

如何让老旧安卓电视焕发新生:mytv-android实现流畅播放体验的完整指南

如何让老旧安卓电视焕发新生&#xff1a;mytv-android实现流畅播放体验的完整指南 【免费下载链接】mytv-android 使用Android原生开发的视频播放软件 项目地址: https://gitcode.com/gh_mirrors/my/mytv-android 你是否还在为家中那台反应迟钝、启动缓慢的旧电视而烦恼…...

HC32F460_ADC驱动(二)

2 ADC工作的核心要素2.1 采样保持一般来说采样保持电路&#xff08;S/H&#xff09;是ADC转换的前端电路。由于模拟信号是时刻连续变化的&#xff0c;若转换过程中输入电压持续波动会导致转换结果失真。采样保持电路的核心作用是在ADC启动转换后保持输入信号不变&#xff0c;保…...

颠覆性网络拓扑可视化:基于Vue+SVG的一站式轻量级解决方案

颠覆性网络拓扑可视化&#xff1a;基于VueSVG的一站式轻量级解决方案 【免费下载链接】easy-topo vuesvgelement-ui 快捷画出网络拓扑图 项目地址: https://gitcode.com/gh_mirrors/ea/easy-topo 在复杂的网络架构设计和运维管理中&#xff0c;网络工程师和开发人员经常…...

Degrees of Lewdity中文本地化完全指南:解决游戏语言障碍的3个实用技巧

Degrees of Lewdity中文本地化完全指南&#xff1a;解决游戏语言障碍的3个实用技巧 你是否因Degrees of Lewdity英文界面而无法深入体验游戏&#xff1f;是否曾因汉化步骤繁琐而放弃尝试&#xff1f;本指南将通过模块化解决方案&#xff0c;帮助你快速完成游戏汉化&#xff0c…...

从零到一:51单片机蓝牙遥控车实战指南(附避坑要点)

1. 项目背景与准备 作为一个非硬件专业的爱好者&#xff0c;我第一次接触51单片机时完全是一头雾水。记得当时因为特殊原因在家闲着&#xff0c;突发奇想做个蓝牙遥控车玩玩。没想到这个简单的想法&#xff0c;让我踩遍了新手能遇到的所有坑。现在回头看&#xff0c;其实用51单…...

员工管理(新增员工)、事务管理和文件上传(阿里云OSS)

员工管理(新增员工) 思路就是就是新增的员工基本信息和批量保存员工的工作经历信息&#xff0c;也就是后端对应了两条sql语句&#xff0c; 1.保存员工基本信息 Emp实体类中新添一个字段用于保存员工工作经历 //封装工作经历 private List<EmpExpr> exprList; (1)Cont…...

艾尔登法环风灵月影修改器下载(已汉化)分享2026最新版

《艾尔登法环》以交界地为舞台&#xff0c;打造了一款兼具开放世界探索与高难度挑战的角色扮演游戏。玩家将扮演褪色者&#xff0c;在破碎的土地上冒险&#xff0c;挑战强大敌人、收集装备、提升能力&#xff0c;最终成为艾尔登之王。游戏以硬核战斗与开放探索为核心&#xff0…...

【限时解密】Google内部测试版Gemini插件Beta通道开放倒计时——附3个已验证的早期功能入口及Token获取密钥

更多请点击&#xff1a; https://intelliparadigm.com 第一章&#xff1a;Gemini Chrome浏览器插件的演进脉络与Beta通道战略意义 Gemini Chrome 插件自 2023 年底首次公开测试以来&#xff0c;已历经三次重大架构重构&#xff1a;从初始的轻量级内容注入脚本&#xff0c;演进…...

对比不同模型在Taotoken平台上的响应速度与输出质量体感

&#x1f680; 告别海外账号与网络限制&#xff01;稳定直连全球优质大模型&#xff0c;限时半价接入中。 &#x1f449; 点击领取海量免费额度 对比不同模型在Taotoken平台上的响应速度与输出质量体感 在开发与创作过程中&#xff0c;我们常常面临一个选择&#xff1a;是追求…...

ClawDrive:为AI智能体设计的语义文件管理与跨模态检索系统

1. 项目概述&#xff1a;ClawDrive&#xff0c;为AI智能体打造的“语义硬盘” 如果你和我一样&#xff0c;每天被海量的文档、图片、音频和视频文件淹没&#xff0c;传统的文件夹分类和文件名搜索早已力不从心。更头疼的是&#xff0c;当你尝试让AI助手&#xff08;比如Claude…...