纯虚函数在C ++中的用途是什么? [英] What are the uses of pure virtual functions in C++?

查看:146
本文介绍了纯虚函数在C ++中的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在学习一个类中的C ++,而且我并不熟悉纯虚函数。我理解他们后来在一个派生类中概述,但是如果你只是在派生类中定义它,你为什么要声明它等于0?


I'm learning about C++ in a class right now and I don't quite grok pure virtual functions. I understand that they are later outlined in a derived class, but why would you want to declare it as equal to 0 if you are just going to define it in the derived class?

解决方案

Briefly, it's to make the class abstract, so that it can't be instantiated, but a child class can override the pure virtual methods to form a concrete class. This is a good way to define an interface in C++.

这篇关于纯虚函数在C ++中的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆