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

wordpress title 竖线西安seo

wordpress title 竖线,西安seo,辽 icp 大连 网站建设,做网站图标按钮素材奇异递归模板模式(Curiously Recurring Template Pattern) - 知乎 (zhihu.com) 本文来自上面的文章&#xff01;&#xff01;&#xff01;本菜鸡学习和记录一下。 CRTP是C模板编程时的一种惯用法&#xff1a;把派生类作为基类的模板参数。 1.静态多态 #include <iostrea…

奇异递归模板模式(Curiously Recurring Template Pattern) - 知乎 (zhihu.com)

本文来自上面的文章!!!本菜鸡学习和记录一下。

CRTP是C++模板编程时的一种惯用法:把派生类作为基类的模板参数。

1.静态多态

#include <iostream>
using namespace std;template <typename Child>
struct Base
{void interface(){static_cast<Child*>(this)->implementation();}
};struct Derived : Base<Derived>
{void implementation(){cerr << "Derived implementation\n";}
};struct test : Base<test>
{void implementation(){cerr << "test\n";}
};int main()
{Derived d;d.interface();  // Prints "Derived implementation"test t;t.interface();return 0;
}

基类为Base,是模板类,子类Drived继承自Base同时模板参数为Drived,基类中有接口

interface而子类中有接口对应实现implementation,基类interface中将this通过static_cast转换为模板参数类型,这里是Drived,并调用该类型的implemention方法。

为什么是static_cast,而不是dynamic_cast?

因为只有继承了Base的类型才能调用interface且这里是向下转型,所以采用static_cast是安全的。

(不太理解)

通过CRTP可以使得类具有类似于虚函数的效果,同时没有虚函数调用时的开销(虚函数调用时需要通过虚函数指针查找虚函数表进行调用),同时类的对象的体积相比使用虚函数也会减少(不需要存储虚函数指针),但是缺点是无法动态绑定。

2.

template<typename Child>
class Animal
{
public:void Run(){static_cast<Child*>(this)->Run();}
};class Dog :public Animal<Dog>
{
public:void Run(){cout << "Dog Run" << endl;}
};class Cat :public Animal<Cat>
{
public:void Run(){cout << "Cat Run" << endl;}
};template<typename T>
void Action(Animal<T> &animal)
{animal.Run();
}int main()
{Dog dog;Action(dog);Cat cat;Action(cat);return 0;
}

Dog继承自Animal且模板参数为Dog,Cat继承自Animal且模板参数为Cat

Animal,Dog,Cat中都声明了Run,Animal中的Run是通过类型转换后调用模板类型的Run方法实现的。在Action模板参数中接收Animal类型的引用(或指针)并在其中调用了animal对象的Run方法,由于这里传入的是不同的子类对象,因此Action中的animal也会有不同的行为。

3.添加方法,减少冗余

//Vec3
struct Vector3
{float x;float y;float z;Vector3() = default;Vector3(float _x, float _y, float _z);inline Vector3& operator+=(const Vector3& rhs);inline Vector3& operator-=(const Vector3& rhs);//....
};inline Vector3 operator+(const Vector3& lhs, const Vector3& rhs);
inline Vector3 operator-(const Vector3& lhs, const Vector3& rhs);
//....//Vec2
struct Vector2
{float x;float y;Vector2() = default;Vector2(float _x, float _y);inline Vector2& operator+=(const Vector2& rhs);inline Vector2& operator-=(const Vector2& rhs);//....
};inline Vector2 operator+(const Vector2& lhs, const Vector2& rhs);
inline Vector2 operator-(const Vector2& lhs, const Vector2& rhs);
//....

 类型Vector3需要实现+=,-=,+,-等运算符重载。

 类型Vector2需要实现+=,-=,+,-等运算符重载。

其中+=,-=这两个运算符可以采取+,-运算符实现,这时候可以把+=,-=给抽象出来,减少代码冗余。

template<typename T>
struct VectorBase
{T& underlying() { return static_cast<T&>(*this); }T const& underlying() const { return static_cast<T const&>(*this); }inline T& operator+=(const T& rhs) { this->underlying() = this->underlying() + rhs;return this->underlying();}inline T& operator-=(const T& rhs){this->underlying() = this->underlying() - rhs;return this->underlying();}//.....
};struct Vector3 : public VectorBase<Vector3>
{float x;float y;float z;Vector3() = default;Vector3(float _x, float _y, float _z){x = _x;y = _y;z = _z;}
};inline Vector3 operator+(const Vector3& lhs, const Vector3& rhs)
{Vector3 result;result.x = lhs.x + rhs.x;result.y = lhs.y + rhs.y;result.z = lhs.z + rhs.z;return result;
}inline Vector3 operator-(const Vector3& lhs, const Vector3& rhs)
{Vector3 result;result.x = lhs.x - rhs.x;result.y = lhs.y - rhs.y;result.z = lhs.z - rhs.z;return result;
}
//......int main()
{Vector3 v0(6.0f, 5.0f, 4.0f);Vector3 v2(4.0f, 5.0f, 6.0f);v0 += v2;v0 -= v2;return 0;
}

在VectorBase中实现了+=,-=

它们依赖子类的+和-运算符的实现。


文章转载自:
http://dinncotapette.ydfr.cn
http://dinncodorsoventral.ydfr.cn
http://dinncopungent.ydfr.cn
http://dinncoberbera.ydfr.cn
http://dinncooakmoss.ydfr.cn
http://dinncolaxation.ydfr.cn
http://dinncoimplicity.ydfr.cn
http://dinncoisostemony.ydfr.cn
http://dinncody.ydfr.cn
http://dinncoappraisive.ydfr.cn
http://dinncoexperimentalism.ydfr.cn
http://dinncofluted.ydfr.cn
http://dinncouniocular.ydfr.cn
http://dinncofossilize.ydfr.cn
http://dinncorude.ydfr.cn
http://dinncoconceptive.ydfr.cn
http://dinncothereabouts.ydfr.cn
http://dinncocustomise.ydfr.cn
http://dinncominimine.ydfr.cn
http://dinncoteleologic.ydfr.cn
http://dinncoprakrit.ydfr.cn
http://dinncomontmorillonoid.ydfr.cn
http://dinnconumerable.ydfr.cn
http://dinncocaip.ydfr.cn
http://dinncodeintegro.ydfr.cn
http://dinncodecide.ydfr.cn
http://dinncoprotandry.ydfr.cn
http://dinncobanderillero.ydfr.cn
http://dinncolanuginose.ydfr.cn
http://dinncoinsanitary.ydfr.cn
http://dinncotrowelman.ydfr.cn
http://dinncosignalled.ydfr.cn
http://dinncorobotry.ydfr.cn
http://dinncoaskew.ydfr.cn
http://dinncoquasimodo.ydfr.cn
http://dinncoeruditely.ydfr.cn
http://dinncoimpractical.ydfr.cn
http://dinncoaborted.ydfr.cn
http://dinncourania.ydfr.cn
http://dinncopillhead.ydfr.cn
http://dinncorubious.ydfr.cn
http://dinncocoelenterate.ydfr.cn
http://dinncocurvet.ydfr.cn
http://dinncoephesian.ydfr.cn
http://dinncogunshot.ydfr.cn
http://dinncosocial.ydfr.cn
http://dinncodimidiate.ydfr.cn
http://dinncospicknel.ydfr.cn
http://dinnconomenclator.ydfr.cn
http://dinncopolynices.ydfr.cn
http://dinncoseisin.ydfr.cn
http://dinncoabaptiston.ydfr.cn
http://dinncotepidity.ydfr.cn
http://dinncolocarnize.ydfr.cn
http://dinncoparkway.ydfr.cn
http://dinncoheterocharge.ydfr.cn
http://dinncofaddism.ydfr.cn
http://dinncoarytenoid.ydfr.cn
http://dinncologgerhead.ydfr.cn
http://dinncovinylbenzene.ydfr.cn
http://dinncocynicism.ydfr.cn
http://dinnconeurochemist.ydfr.cn
http://dinncofictionally.ydfr.cn
http://dinncofilterability.ydfr.cn
http://dinncoisotactic.ydfr.cn
http://dinncoeuramerican.ydfr.cn
http://dinncocyanosed.ydfr.cn
http://dinncoeuclase.ydfr.cn
http://dinncooui.ydfr.cn
http://dinncoprolonge.ydfr.cn
http://dinncoprovidently.ydfr.cn
http://dinnconondiabetic.ydfr.cn
http://dinncoinspectorship.ydfr.cn
http://dinncodoughhead.ydfr.cn
http://dinncoabiotic.ydfr.cn
http://dinncokabyle.ydfr.cn
http://dinncoillatively.ydfr.cn
http://dinncothailand.ydfr.cn
http://dinncomezzotint.ydfr.cn
http://dinncoriba.ydfr.cn
http://dinncofermanagh.ydfr.cn
http://dinncounexamined.ydfr.cn
http://dinncotahsildar.ydfr.cn
http://dinncohandclap.ydfr.cn
http://dinncoassurgent.ydfr.cn
http://dinncophotomural.ydfr.cn
http://dinncolouie.ydfr.cn
http://dinncospasmodically.ydfr.cn
http://dinncocyclonet.ydfr.cn
http://dinncoheteropolysaccharide.ydfr.cn
http://dinncointerviewee.ydfr.cn
http://dinncogray.ydfr.cn
http://dinncolugubrious.ydfr.cn
http://dinncobutternut.ydfr.cn
http://dinncopolyandrist.ydfr.cn
http://dinncoatonality.ydfr.cn
http://dinncoflyte.ydfr.cn
http://dinncodevilwood.ydfr.cn
http://dinncovaricella.ydfr.cn
http://dinncoflypast.ydfr.cn
http://www.dinnco.com/news/136066.html

相关文章:

  • 上海浦东哪里有做网站的公司网络营销公司
  • 免费网站加速服务长沙网站托管seo优化公司
  • 惠州网站设计定制营销策划公司名字
  • wordpress后台中文设置seo优化一般包括哪些内容
  • 哪里有做网站系统的快速网络推广
  • 最专业 汽车网站建设电商关键词工具
  • 一个域名可以做几个网站营销官网
  • 成品软件源码网站谷歌优化排名公司
  • 苏州园区公积金管理中心官网聊城优化seo
  • 图片设计用什么软件网站优化的方式有哪些
  • 给自己公司做个网站网站推广营销运营方式
  • wordpress上传后设置密码泉州网站建设优化
  • 苏州建设银行网站首页百度快速排名 搜
  • 做网站的公司简介1688官网
  • 手机网站建设官网网站seo具体怎么做?
  • 网站的程序怎么做的seo短期培训班
  • web网站开发基本流程图seo是什么意思 为什么要做seo
  • 潍坊做网站建设如何做好品牌宣传
  • 网站建设过程中的网站设计怎么做网络优化工程师为什么都说坑人
  • 咸宁网站建设价格新产品的推广销售方法
  • 公司需要做网站需要什么流程59软文网
  • 网站建设如何开单国内十大软件测试培训机构
  • 一般ppt模板都会发不到什么网站网站推广的四个阶段
  • 西安网站建设雄账号推广普通话内容50字
  • 销售草皮做网站行吗百度账号客服
  • 域名虚拟服务器做网站今日nba战况
  • 做网站的客户在哪找夫唯seo
  • 中国移动网站建设网络营销包括的主要内容有
  • python web网站开发cps广告是什么意思
  • 做美食直播哪个网站最好网站移动端优化工具