当前位置:网站首页>March 11, 2022 diary: Mr. Wang's spring, strange template mode
March 11, 2022 diary: Mr. Wang's spring, strange template mode
2022-06-12 23:48:00 【Tuen Mun pheasants call me chicks】
Recommend a Zero sound College Free public courses , Personally, I think the teacher spoke well , Share with you :[Linux,Nginx,ZeroMQ,MySQL,Redis,fastdfs,MongoDB,ZK, Streaming media ,CDN,P2P,K8S,Docker,TCP/IP, coroutines ,DPDK Etc , Learn now ]
Background server :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() Yes " << endl;
}
//......
};
Case study 1 It can be noted that ,Base Class T& derived = static_cast<T&>(*this); this It's a little weird , At least it seems strange to me , It is a template T Type variables are placed in Base Class , And then call T Functions of type objects ,
Derived1 myd;
myd.asDerived(); // Call the member function of the base class
stay main Function is called in this way , It really looks like a monk , It's confusing , It's very strange .
DEMO2
template<typename T>
struct shape
{
// The judgment of whether the derived class objects are equal is moved to the base class ( Using the method of defining friend functions in class templates, the global operator== Put in base class )
friend bool operator==(const shape<T>& obj1, const shape<T>& obj2) // Define friends in the class template
{
const T& objtmp1 = static_cast<const T&>(obj1);// Derived class objects are also base class objects , So this static type conversion is OK
const T& objtmp2 = static_cast<const T&>(obj2);
if (!(objtmp1 < objtmp2) && !(objtmp2 < objtmp1))
return true;
return false;
}
};
struct square : public shape<square>
{
int sidelength; // Side length
};
// Out of class operator overload
bool operator<(square const& obj1, square const& obj2)
{
if (obj1.sidelength < obj2.sidelength)
{
return true;
}
return false;
}
Case study 2 It's a case 1 A derivative of , But there is operator Knowledge of overloading and friends . Collect this knowledge first , I know there's such a thing , Maybe you can use it when you have time or have a whim in the future !
边栏推荐
- How does idea switch the interface to Chinese
- Is divicon still used in leaflet in H5 era?
- Is the stock account opened by qiniu Gang safe and reliable?
- How to publish OSM maps locally and customize the mapping
- leaflet中如何优雅的解决百度、高德地图的偏移问题
- Mgr and greatsql resource summary
- Software development tools [3] theoretical basis of software development tools
- 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
- Pytorch loading model error resolution
- Start of u-boot_ Armboot analysis (II)
猜你喜欢

Start of u-boot_ Armboot analysis (I)

Redis实现短信验证码登录

Pytorch loading model error resolution

H5時代leaflet中還在用DivIcon?
![Buuctf-[ciscn 2019 preliminary]love math](/img/56/dbe7fc1d5a55551153e3df39810856.jpg)
Buuctf-[ciscn 2019 preliminary]love math
![[matlab] two dimensional curve](/img/5a/bd69c78513fe4b59b2c63dc3679841.png)
[matlab] two dimensional curve

Unprecedented analysis of Milvus source code architecture

How to publish OSM maps locally and customize the mapping

Heilongjiang Branch and Liaoning Branch of PostgreSQL Chinese community have been established!

如何让矢量瓦片配图神器maputnik支持 geoserver
随机推荐
Tableau
OSM地图本地发布-如何生成各省市矢量地图
Hongmeng starts 2
Heilongjiang Branch and Liaoning Branch of PostgreSQL Chinese community have been established!
〖Kubernetes指南④〗Pod快速入门
Develop a web office suite from scratch (5): mouse hover over text
华为云弹性云服务器ECS使用【华为云至简致远】
H5時代leaflet中還在用DivIcon?
Leetcode 2164. Sort odd and even subscripts separately (yes, once)
CV - baseline summary (development history from alexnet to senet)
2022年危險化學品經營單比特安全管理人員考試試題及在線模擬考試
AWS lambda: how to store secrets to external APIs- AWS Lambda: How to store secret to external API?
OSM map local publishing - how to generate vector maps of provinces and cities
[opencv learning] small ticket recognition based on perspective transformation and OCR recognition
Unprecedented analysis of Milvus source code architecture
2022起重机械指挥上岗证题目模拟考试平台操作
scala中的隐式转换和隐式参数讲解与实践
CS for mobile security [nethunter]
Access static variables within class in swift
How to use Huawei cloud disaster tolerance solution to replace disaster recovery all-in-one machine