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

企业网站源代码免费下载情人节网站怎么做

企业网站源代码免费下载,情人节网站怎么做,中标查询,苏州网站建设找苏州聚尚网络首选奇异递归模板模式(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_castChild*(this)-implementation();} };struct Derived : BaseDerived {void implementation(){cerr Derived implementation\n;} };struct test : Basetest {void implementation(){cerr test\n;} };int main() {Derived d;d.interface(); // Prints Derived implementationtest 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. templatetypename Child class Animal { public:void Run(){static_castChild*(this)-Run();} };class Dog :public AnimalDog { public:void Run(){cout Dog Run endl;} };class Cat :public AnimalCat { public:void Run(){cout Cat Run endl;} };templatetypename T void Action(AnimalT animal) {animal.Run(); }int main() {Dog dog;Action(dog);Cat cat;Action(cat);return 0; } Dog继承自Animal且模板参数为DogCat继承自Animal且模板参数为Cat Animal,Dog,Cat中都声明了RunAnimal中的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需要实现,-,,-等运算符重载。 其中-这两个运算符可以采取,-运算符实现这时候可以把,-给抽象出来减少代码冗余。 templatetypename T struct VectorBase {T underlying() { return static_castT(*this); }T const underlying() const { return static_castT 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 VectorBaseVector3 {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://www.hkea.cn/news/14312587/

相关文章:

  • 网站怎样排版上海龙雨建设工程有限公司网站
  • wordpress网站被自动跳转wordpress备份博客图片
  • 网站建设排名公司哪家好wordpress是免费吗
  • 深圳做网站最电商联盟推广
  • 网站建设的客户都在哪里移动网站建站
  • 做网站组服务器鹤岗网站seo
  • 织梦后台做的网站怎么绑定域名服务好的企业网站怎么建设
  • 商务网站构建与维护wordpress 主题依赖插件
  • 洮南网站正规的扬中网站建设
  • 徐州网站建设找哪家上海企业信息查询系统
  • 江苏网站建设基本流程wordpress流水布局主题
  • 四川省微信网站建设推广dw个人网页制作素材
  • 主题资源网站建设模块五作业企业网站能提供哪些服务
  • 廊坊网站群建设那个网站可以免费做风面
  • 利用云服务器做网站wordpress使用html5
  • 高端大气网站设计欣赏互联网营销师报名官网
  • 漯河公司做网站wordpress 变装小说
  • 网站推广外包北京西站在几环
  • 苏州手机网站建设报价网站建设哪里公司好
  • 乌尔禾区做网站哪里好校园网站建设案例
  • 平面设计师必看的网站建设部网站资质核查
  • 北京网站建设排行廊坊做网站找谁
  • 生鲜网站建设的总体目标有哪个网站可以做链接
  • 答辩的时间_老师问了我做的网站可以同时支持的并发用户是多少用了wordpress的电商网站
  • 企业门户网站建设情况wordpress免签约
  • asp网站首页模板ps详情页模板
  • 如何做能上传视频网站城乡建设部网站首页
  • 公司网站设计素材网页设计咨询
  • 60天做网站wordpress 域名更换
  • C语言网站开发pdfphp做网站图集