当前位置:网站首页>2022年3月11日记:王老师的春天,奇异的模板模式
2022年3月11日记:王老师的春天,奇异的模板模式
2022-06-12 23:41:00 【屯门山鸡叫我小鸡】
推荐一个 零声学院 免费公开课程,个人觉得老师讲得不错,分享给大家:[Linux,Nginx,ZeroMQ,MySQL,Redis,fastdfs,MongoDB,ZK,流媒体,CDN,P2P,K8S,Docker,TCP/IP,协程,DPDK等技术内容,立即学习]
后台服务器:https://course.0voice.com/v1/course/intro?courseId=5&agentId=0
DEMO1
template <typename T>
class Base
{
public:
void asDerived()
{
T& derived = static_cast<T&>(*this);
derived.myfunc();
}
private:
Base() {
};
friend T;
};
class Derived1 : public Base<Derived1>
{
public:
void myfunc()
{
cout << "Derived1::myfunc()执行了" << endl;
}
//......
};
案例1中可以注意到,Base类中的 T& derived = static_cast<T&>(*this);这 写法有点怪异,至少我看着是有些奇怪,它是把模板T类型的变量放在Base类中,然后去调用T类型对象的函数,
Derived1 myd;
myd.asDerived(); //调用基类的成员函数
在main函数中调用是这样调用的,真的是看起来丈二和尚,让人摸不清头脑,非常的奇怪。
DEMO2
template<typename T>
struct shape
{
//把派生类对象是否相等的判断挪到了基类中(使用了在类模板中定义友元函数的手段把全局的operator==放到基类中)
friend bool operator==(const shape<T>& obj1, const shape<T>& obj2) //在类模板中定义友元
{
const T& objtmp1 = static_cast<const T&>(obj1);//派生类对象也是基类对象,所以这种静态类型转换没问题
const T& objtmp2 = static_cast<const T&>(obj2);
if (!(objtmp1 < objtmp2) && !(objtmp2 < objtmp1))
return true;
return false;
}
};
struct square : public shape<square>
{
int sidelength; //边长
};
//类外运算符重载
bool operator<(square const& obj1, square const& obj2)
{
if (obj1.sidelength < obj2.sidelength)
{
return true;
}
return false;
}
案例2是案例1的一个衍生品,但是里面加入了operator重载和友元方面的知识。这块知识先收藏下来,知道有这么个东西,等日后有时间或者突发奇想的时候也许可以用得上!
边栏推荐
- 2202-簡曆制作
- CS for mobile security [nethunter]
- Record 5 - the serial port of stm32f411ceu6 realizes the sending and receiving of fixed length data and variable length data
- 妙才周刊 - 5
- [North Asia data recovery] data recovery cases in which the partitions disappear and the partitions are inaccessible after the server reinstalls the system
- 设计消息队列存储消息数据的 MySQL 表格
- Summary of individual NLP internship experience
- 基于Three.js海上风电数字孪生三维效果
- CV—BaseLine总结(从AlexNet到SENet的发展历程)
- SAP UI5 如何通过 manifest.json 文件定义第三方库依赖关系
猜你喜欢

2022年G3锅炉水处理考题模拟考试平台操作
![Leetcode 890 finding and replacing patterns [map] the leetcode path of heroding](/img/a2/186439a6d50339ca7f299a46633345.png)
Leetcode 890 finding and replacing patterns [map] the leetcode path of heroding

The Milvus graphical management tool Attu is coming!

MySQL基础篇视图的总结

dict和set的基本操作

數組

2202-简历制作

Alien Skin Exposure X7调色滤镜插件,RAW后期处理工具

Enterprise wechat H5_ Authentication, H5 application web page authorization login to obtain identity

OSM地图本地发布-如何生成各省市矢量地图
随机推荐
Face detection: mtcnn
Matters of parent-child class construction method in inheritance
How about opening a securities account in flush? Is it safe or not
2022起重机械指挥上岗证题目模拟考试平台操作
House raiding 2
Bit_ Binary number
How does idea switch the interface to Chinese
Find out the data that can match the keyword key in field 1 or field 2 in the database table. If you want to display the matching data in field 1 first
[redis sentinel] failed listening on port 26379 (TCP) & sentinel mode no response problem solved
Hongmeng starts 2
It is meaningful to define genus, and D can use it to explain semantics
leaflet如何优雅的展示重叠点位的气泡窗口
支持Canvas的Leaflet.Path.DashFlow动态流向线
Redis realizes SMS verification code login
2022年危險化學品經營單比特安全管理人員考試試題及在線模擬考試
[opencv learning] use the Tesseract OCR movement to recognize numbers
CS for mobile security [nethunter]
SAP UI5 如何通过 manifest.json 文件定义第三方库依赖关系
[opencv learning] small ticket recognition based on perspective transformation and OCR recognition
36 krypton's debut | "osogena" won nearly ten million angel rounds of financing. The original DLR scientists of German Aerospace Research and development system modeling and simulation CAE software PA