我举一个最简单的例子:比如现在有两个: Triangle和Circle图形,他们都是shape类型。现在我们想要统一管理这些几何形状图形。我们应该怎么做了? 显然有两种典型的设计模式:继承和模板。
继承的实现往往是一个抽象的基类,这个基类的作用往往是提供接口,往往不会真正的去实现,比如:我们想要 Triangle 和 Circle 都有一个查询面积的接口,我们可以这样实施一个基类。
- class Shape {
-
- public:
- double get_area();
- }
这样就有一个统一的接口,如果我们的 Circle类和Rectangle 类, public 继承Shape类,那么他们就可以得到 Shape类的这个函数
- // Circle
-
- public Circle :public Shape {
-
- public:
- double get_area();
- }
这个时候,父类和子类的 get_area()都有着良好的定义,而且是可以被调用的。
一般而言,调用的是哪个函数(父类或者子类),会取决于指针是什么类型,这当然在编译期就能确定,但是你没有办法 用 shape* 类型的指针来管理 Circle 或者 Tranigle。比如:
- Circle *c = new Circle();
- c->get_area() ; // 在编译期就 确定是调用 : Citcle::get_area()
-
-
- Shape *s1 = new Circle();
- s1->get_area() ; // 编译期就 确定调用: Shape::get_area() , 但是目的是想调用
-
- // Circle::get_area() ,但是 指针 Shape *s1 没有管理 Circle
-
- Shape *s2 = new Shape();
- s2->get_area(); // 在编译期 就确定调用 : Shape::get_area()
那怎么解决这个问题了 ?C++ 提出了 虚函数的概念
Shape 类有没有这个接口, 而在运行时会查虚函数表, 才决定具体调用哪个现在我们将刚刚那个例子重新修改下:
- class Shape {
-
- public:
- virtual double get_area();
- };
-
-
- class Circle: public Shape{
- public:
- virtual double get_area();
- }
-
-
- Circle *c = new Circle;
- c->get_area(); // 运行时调用了 Circle::get_area()
-
- Shape *s1 = new Circle;
- s1->get_area(); // 运行时调用了 Circle::get_area()
-
- Shape *s2 = new Shape;
- s2->get_area(); // 运行时调用了 Shape::get_area()
很显然:将函数声明为虚函数之后, shape *s1指针就可以管理Circle类了,它可以直接调用:Circle::get_area()函数。(它在运行期,会查虚函数表,根据指针具体类型,决定调用那个函数)
在实践中,通常我们需要禁止抽象类的接口调用,也就是禁止调用 Shape::get_area()函数,那么为了实现这一点,我们需要把抽象类声明成 纯虚函数。这样就可以防止新建抽象类的实例,进而防止调用抽象类的函数了。纯虚函数声明 :virtual 函数类型 函数名 (参数表列) = 0;
(1)纯虚函数没有函数体;
(2)最后面的“=0”并不表示函数返回值为0,它只起形式上的作用,告诉编译系统“这是虚函数”;
(3)这是一个声明语句,最后有分号。
- // Shape.cpp
- #pragma once;
- class Shape
- {
- public:
- virtual double get_area() = 0;
- };
-
-
- // Circle.cpp
- #include"Shape.cpp"
-
- class Circle: public Shape
- {
- public:
- virtual double get_area() {
- return 10.00;
- }
- };
-
-
- // main.cpp
- #include
- #include"Circle.cpp"
- #include"Shape.cpp"
-
- int main() {
- Shape* s = new Circle();
- std::cout << s->get_area() << std::endl;
-
- Circle* c = new Circle();
- std::cout << c->get_area() << std::endl;
- return 0;
- }
-
- // 打印结果:
- 10
- 10
-
-
以上就是通过继承(定义虚函数)的方法来实现这个需求,但是虚函数的调用,必然又会去查虚函数表,既然涉及到查表,必然会有一定的性能开销,为此我们可以通过模板 Template来实现。
除了通过继承关系实现多态之外,C++还提供了一种方式来实现这个需求:模板。
- // 定义枚举
- enum Shapename {
- CIRCLE, //枚举默认是0
- TRIANGLE, // 默认 1
- RECTANGLE // 默认2
-
- }
-
-
- // 定义一个模板
- template
- Class Shape;
-
- // 模板特化
- template<>
- class Shape
{ - // ......
-
- }
-
完整代码
- #include
-
- enum Shapename
- {
- CIRCLE, //圆形
- TRIANGLE, //三角形
- RECTANGLE // 长方形
- };
-
- // 定义一个模板
- template
- class Shape;
-
- // 特化Shape
// template<> 告诉编译器需要特化下面这个类 - template<>
- class Shape
- {
- private:
- double radius = 1;
- public:
- double get_area() {
- return 3.1415926 * radius * radius;
- }
-
- };
-
- // 特化Shape
// template<> 告诉编译器需要特化下面这个类 - template<>
- class Shape
- {
- private:
- double a = 1, b = 2;
- public:
- double get_area() {
- return a * b;
- }
- };
-
- // 特化Shape
// template<> 告诉编译器需要特化下面这个类 - template<>
- class Shape
- {
- private:
- double a = 1, b = 2;
- public:
- double get_area() {
- return a * b / 2;
- }
- };
-
- // 定义统一接口
- // 声明模板类型 template
, Shapename是一个类模板,sp是形参可以是任意名字 - // 这样我们就搞出一个统一接口(模板函数)来处理形状面积了。
- template
- // 函数的形参不能和模板的形参同名
- double get_area(Shape
* shape) { - return shape->get_area();
- }
-
-
- int main() {
- Shape
* c = new Shape; - Shape
* r = new Shape; - Shape
* t = new Shape; - std::cout << "CIRCLE area:" << get_area(c) << std::endl;
- std::cout << "RECTANGLE area:" << get_area(r) << std::endl;
- std::cout << "TRIANGLE area:" << get_area(t) << std::endl;
- return 0;
- }
-
-
- // 打印结果
- CIRCLE area:3.14159
- RECTANGLE area:1
- TRIANGLE area:2
好了,至此,我们分析完继承和模板在 C++中使用场景和应用。在大型C++项目中,一般会将继承和模板结合起来使用,从而衍生出 CRTP (curious recurring template pattern) 设计模式,并
可以用来给c++的class提供额外功能、实现静态多态等。
关于 CRTP这个介绍,我在下一篇博客进行详细的分析。