相关内容:
- 在
C++
中,可以将虚函数声明为纯虚函数,语法格式为:virtual 返回值类型 函数名 (函数参数) = 0;
- 纯虚函数没有函数体,只有函数声明,在虚函数声明的结尾加上
=0
,表明此函数为纯虚函数。最后的=0
并不表示函数返回值为0
,它只起形式上的作用,告诉编译系统“这是纯虚函数”。 - 包含纯虚函数的类称为抽象类
(Abstract Class)
。之所以说它抽象,是因为它无法实例化,也就是无法创建对象。原因很明显,纯虚函数没有函数体,不是完整的函数,无法调用,也无法为其分配内存空间。