在 C++ 中,虚函数和纯虚函数的概念都与多态和接口实现有关。它们的主要区别在于它们的定义和用途。
虚函数(Virtual Function)
虚函数是在基类中使用 virtual 关键字声明的函数,它的目的是允许派生类(子类)重写(override)该函数的行为。当你通过基类指针或引用调用虚函数时,C++ 运行时会使用动态绑定(late binding)来确定应当调用哪个类的函数实现。虚函数允许基类提供默认的实现,而不需要强制派生类去重写它。
基类 Base 有一个虚函数 Display()。Derived1 和 Derived2 继承自 Base 并覆盖了 Display() 函数。在 main 函数中,我们使用 Base 类型的指针来指向派生类 Derived1 和 Derived2 的对象,并且调用 Display() 函数。通过动态绑定,程序能在运行时决定调用 Derived1 或 Derived2 的 Display() 实现,而不是 Base 类中的实现。
#include
#include
class Base {
public:
virtual void Display() {
std::cout << "Display Base class" << std::endl;
}
virtual ~Base() {}
};
class Derived1 : public Base {
public:
void Display() override {
std::cout << "Display Derived1 class" << std::endl;
}
};
class Derived2 : public Base {
public:
void Display() override {
std::cout << "Display Derived2 class" << std::endl;
}
};
int main() {
#if 1
std::unique_ptr<Base> basePtr;
basePtr = std::make_unique<Derived1>();
basePtr->Display();
basePtr = std::make_unique<Derived2>();
basePtr->Display();
#else
Base* b;
Derived1 d1;
Derived2 d2;
b = &d1;
b->Display();
b = &d2;
b->Display();
#endif
return 0;
}
注意,一个好的实践是在基类中提供虚析构函数。如果你的基类有虚函数,那么它很可能会被用作多态基类,这意味着人们可能会用基类指针指向派生类的对象,并期望通过基类指针来删除这些对象。在这种情况下,如果基类的析构函数不是虚的,那么派生类的析构函数就不会被调用,可能导致资源泄漏或其他问题。
纯虚函数(Pure Virtual Function)
纯虚函数是在基类中声明的一种特殊类型的虚函数,它没有定义(或者说是定义为0),用于创建抽象类。如果类中至少有一个纯虚函数,则该类被认为是抽象类,它不能被实例化。派生类必须重写(override)所有的纯虚函数,才能实例化对象。纯虚函数用于定义接口,在基类中指定派生类必须实现的函数。
#include
using namespace std;
class Shape {
public:
virtual void draw() = 0;
virtual ~Shape() {}
};
class Circle : public Shape {
public:
void draw() override {
cout << "Drawing a circle." << endl;
}
~Circle() {}
};
class Rectangle : public Shape {
public:
void draw() override {
cout << "Drawing a rectangle." << endl;
}
~Rectangle() {}
};
int main() {
Shape *shape;
Circle circle;
Rectangle rectangle;
shape = &circle;
shape->draw();
shape = &rectangle;
shape->draw();
return 0;
}
当你声明一个纯虚函数时,你告诉使用这个类的开发者:这个函数没有默认实现,必须由派生类提供具体实现。
注意
1、纯虚函数不提供任何实现: 在基类中,纯虚函数不需要(也不能)写出具体的函数体。
2、抽象类不能被实例化: 既然Shape类包含了纯虚函数,你无法创建Shape类的对象。
3、派生类必须实现所有纯虚函数: 任何从抽象类派生的类必须实现基类中所有的纯虚函数,除非它们本身也想成为抽象类。
4、可以有虚析构函数: 即使是抽象类,也可以(实际上经常)含有虚析构函数。如果没有虚析构函数,那么通过基类指针删除派生类对象时可能不会调用派生类的析构函数,导致派生类中的资源泄露。
5、带纯虚函数的类可以有成员函数实现: 一个常见的误解是抽象类只能含有纯虚函数,但实际上抽象类也可以包含正常的成员函数和成员变量,包含纯虚函数意思是该类至少包含一项功能必须由派生类提供具体实现。
6、纯虚析构函数必须提供实现: 尽管纯虚析构函数会使类变为抽象类,但你仍然必须为它提供实现。因为当派生类的对象被销毁时,析构函数的调用链会确保基类的析构函数也被调用。
或者说
#include
#include
class Shape {
public:
virtual void draw() const = 0;
virtual ~Shape() {}
};
class Circle : public Shape {
public:
void draw() const override {
std::cout << "Circle drawn." << std::endl;
}
};
class Rectangle : public Shape {
public:
void draw() const override {
std::cout << "Rectangle drawn." << std::endl;
}
};
void drawShapes(const std::vector<Shape*>& shapes) {
for (const auto& shape : shapes) {
shape->draw();
}
}
int main() {
Circle circle;
Rectangle rectangle;
std::vector<Shape*> shapes = {&circle, &rectangle};
drawShapes(shapes);
return 0;
}
总结
虚函数是基类中使用 virtual 关键字声明的成员函数,可以在派生类中被重写,也可以不被重写,如果不被重写,则使用基类的实现。
纯虚函数是一个没有实现的虚函数,它在其声明末尾使用 = 0 表明它是纯虚函数。声明纯虚函数的类是抽象类,不能被实例化,任何继承此类的派生类都必须提供纯虚函数的实现才能实例化。
在派生类中,可以只重写部分虚函数,而其他的不重写;但是纯虚函数必须全部被重写。