why pure virtual function has definition 为什么可以在基类中实现纯虚函数
看了会音频,无意搜到一个frameworks/base/include/utils/Flattenable.h : virtual ~Flattenable() = 0;
所以查了下“纯虚函数定义实现”,下文讲的非常好:
引述自:http://forums.codeguru.com/showthread.php?356281-C-why-pure-virtual-function-has-definition-Please-look-into-sample-code-here
Question C++: why pure virtual function has definition?Please look into sample code here ksrameshkanth
--------------------------------------------------------------------------------------------------------------------------------
Here the class Base has pure virutual function, But it is allowing to have definition,
even it can be called from drived class. There is no compilation error and Run time error.
why?
Anyone Can please give details about it?
Is there any special purpose to allow this? #include <iostream.h>
class Base
{
public:
virtual void Fun( )=; //纯虚函数接口
}; //纯虚函数实现
void Base::Fun(void)
{
cout << "\n I am in Pure virtual function\n";
} class Derived:Public Base
{
public:
void Fun()
{
cout<<"\n I am in the Derived class";
Base::Fun( );//显式调用了基类中的纯虚函数
}
};
void main()
{
Derived d;
Base *b = &d;
b->Fun();
} ------------------------------------------------------------------------------------------------------------------------------- panayotisk
--------------------------------------------------------------------------------------------------------------------------------
If I remember well, somewhere in "Effective C++" Meyers mentions a reason for a pure virtual function to have a body:
Derived classes that implement this pure virtual function may call this implementation smwhere in their code.
If part of the code of two different derived classes is similar then it makes sense to move it up in the hierarchy,
even if the function should be pure virtual.
在<<Effective C++>>(没有拜读过)讲明,派生类可以显式地调用基类中的纯虚函数,这样就可以将将不同子类中公共的事务放在父类中完成
-------------------------------------------------------------------------------------------------------------------------------- ksrameshkanth
--------------------------------------------------------------------------------------------------------------------------------
Thanks for your quick response. I agree with your point that the pure virtual function purpose is to force the derived class to
override it.But my doubt if we are able to call that function from the derived classes,
why are we not allowed to create an object for that class and call that function with that object.
-------------------------------------------------------------------------------------------------------------------------------- panayotisk
--------------------------------------------------------------------------------------------------------------------------------
Using implementation code from a base class has nothing to do with allowing
creation of objects of the base class. These are totally unrelated.
When designing ask yourself:
- Do I need to be able to instantiate objects of the base class?
If not the base class should be abstract (contains at least one pure virtual function).
- Is there common code that derived classes may want to use?
Then consider providing this in the body of a pure virtual function.
在派生类中显式地调用基类中纯虚函数(基类做了定义)与基类能否实例化没有任何关系
-------------------------------------------------------------------------------------------------------------------------------- humptydumpty
--------------------------------------------------------------------------------------------------------------------------------
Purpose of a Pure Virtual Function is to make a Class to Abstract Class.
Often in a design, you want the base class to present only an interface for
its derived classes. That is, you don’t want anyone to actually create an
object of the base class, only to upcast to it so that its interface can be
used. This is accomplished by making that class abstract, which happens if
you give it at least one pure virtual function. You can recognize a pure
virtual function because it uses the virtual keyword and is followed by = 0.
If anyone tries to make an object of an abstract class, the compiler
prevents them. This is a tool that allows you to enforce a particular design.
纯虚函数时为了定义一个抽象类,在该类中必有一些虚函数为纯虚函数,从而只声明了一个接口,而在某个派生类中必然实现该接口 When an abstract class is inherited, all pure virtual functions must be
implemented, or the inherited class becomes abstract as well. Creating a
pure virtual function allows you to put a member function in an interface
without being forced to provide a possibly meaningless body of code for
that member function. At the same time, a pure virtual function forces
inherited classes to provide a definition for it.
纯虚函数的意义在于不可以实例化该类,且不用写"{}"这样无意义的东西,并且要求其某个派生类必然要实现该接口
-------------------------------------------------------------------------------------------------------------------------------- Graham
--------------------------------------------------------------------------------------------------------------------------------
If you have a "normal" virtual function, its implementation (body) becomes
a sort of "default" implementation. That is, derived classes will inherit
that implementation and use it if they don't override the function.
This may or may not be a problem if the author of a derived class forgets to
override a particular virtual function. By making the original function pure (but still with a body), you are saying to
the author of a derived class
"there's a default implementation of this function;
if you want to use, you will have to be explicit about it".
对于一般的虚函数,其实现是作为派生类的默认实现,即若派生类没有override基类中实现,则默认调用
基类中函数体;
但是如果在父类中实现了纯虚函数(接口)的函数体,则此时对于派生类意味着:
在父类中实现了接口的函数体,如果需要使用,请显式地调用。 code:
class base
{
public:
virtual void f() = ;
}; void base::f()
{
// implementation
} class derived : public base
{
public:
virtual void f()
{
base::f(); // explicit use of default implementation
}
};
-------------------------------------------------------------------------------------------------------------------------------- exterminator
--------------------------------------------------------------------------------------------------------------------------------
There is a case where it becomes a necessity to provide the implementation for a pure virtual functions
and that would be a pure virtual destructor.//纯虚析构函数 ISO C++ 12.4 (7):
A destructor can be declared virtual (10.3) or pure virtual (10.4);
if any objects of that class or any derived class are created in the program,
the destructor shall be defined. If a class has a base class with a virtual destructor,
its destructor (whether user or implicitly declared) is virtual.
析构函数可以为虚函数或纯虚函数,如果基类或其派生类被实例化,则析构函数必须定义(当然,对于有纯虚函数的类不能被实例化),
如果基类的析构函数为虚函数,则派生类的析构函数无论是否显示的声明为virtual,均为需函数 Also, its not that since it has a body you need to call it explicitly.
Its the other way round. If it needs to be called, then its body need to be implemented.
The standard states it clearly: ISO C++ 10.4 (2):
A pure virtual function need be defined only if explicitly called with the qualified id syntax (5.1).
如果在派生类中显式的调用基类中的纯虚函数,则基类必须实现纯虚函数接口的函数体 And hence when the call from the derived destructor is done while polymorphic destruction
you need to have a body of the base pure virtual destructor implemented for it to be executed while Base's destruction.
由于在派生类实例被析构时会调用基类的析构函数(编译器完成),因此当我们把基类的析构函数声明为纯虚函数时,必须implement纯虚析构函数的函数体-------------------------------------------------------------------------------------------------------------------------------- Graham
--------------------------------------------------------------------------------------------------------------------------------
I would be happy with the statement if it's specified that he never puts data in an abstract class. Some people stick to this rule - I don't see the point of being that restrictive. It comes down to whether you only ever use abstract classes to mimic "interfaces", or whether you see them as part of a broader picture, one that sees a use for abstract classes with state information.
-------------------------------------------------------------------------------------------------------------------------------- SuperKoko
--------------------------------------------------------------------------------------------------------------------------------
I already had abstract classes with a few fields, used by non-virtual methods who called other virtual methods.
I think that it is a restriction of the language to never put data in abstract classes, and it may need some duplicate code and data member... what is not good.
-------------------------------------------------------------------------------------------------------------------------------- 请教了下师弟关于Java中知识,顺便几下(不会Java,太弱了):
Java中的Interface类相当于C++中的:1、抽象类,函数全为纯虚函数;2、没有数据成员
Java中的抽象类与C++的抽象类相当;
Java中的派生类只能继承一个(抽象)类、可以继承多个接口类
why pure virtual function has definition 为什么可以在基类中实现纯虚函数的更多相关文章
- C++ //纯虚函数和抽象类 // 语法 virtual 返回值类型 函数名 (参数列表)=0 //当类中有了纯虚函数 这个类也称为抽象类
1 //纯虚函数和抽象类 2 // 语法 virtual 返回值类型 函数名 (参数列表)=0 3 //当类中有了纯虚函数 这个类也称为抽象类 4 5 6 #include <iostream& ...
- 不要在基类析构函数中调用纯虚函数,否则运行时会报错“pure virtual method called”
如上. 这是因为:delete派生类对象时,先调用派生类的析构函数,然后再调用基类的析构函数:此时如果调用纯虚函数的话,派生类的对象已经被破坏了,所以会报错. http://www.cnblogs.c ...
- C++虚函数virtual,纯虚函数pure virtual和Java抽象函数abstract,接口interface与抽象类abstract class的比较
由于C++和Java都是面向对象的编程语言,它们的多态性就分别靠虚函数和抽象函数来实现. C++的虚函数可以在子类中重写,调用是根据实际的对象来判别的,而不是通过指针类型(普通函数的调用是根据当前指针 ...
- C++ 纯虚函数与抽象类——virtual 和纯说明符 “=0”
什么时候使用纯虚函数 某些类,在现实角度和项目角度都不需要实例化(不需要创建它的对象),这个类中定义的某些成员函数只是为了提供一个形式上的接口,准备上子类来做具体的实现.此时这个方法就可以定义为&qu ...
- pure virtual function call
2015-04-08 10:58:19 基类中定义了纯虚函数,派生类中将其实现. 如果在基类的构造函数或者析构函数中调用了改纯虚函数, 则会出现R6205 Error: pure virtual fu ...
- Mindjet MindManager 2012 从模板创建出现“Runtime Error pure virtual function call” 解决方法
我的Mindjet MindManager 2012 Pro也就是MindManager10 在应用模板之后总会显示 Microsoft Visual C++ Runtime Library Runt ...
- 结合实例详解"pure Virtual function called"
作者:阿波 链接:http://blog.csdn.net/livelylittlefish/article/details/9750593 (4年前的一篇文章,翻出来共享一下.) 本实例即为经典的讲 ...
- [C++] Pure Virtual Function and Abstract Class
Pure Virtual Function Abstract Class
- 纯虚函数(pure virtual function )和抽象类(abstract base class)
函数体=0的虚函数称为“纯虚函数”.包含纯虚函数的类称为“抽象类” #include <string> class Animal // This Animal is an abstract ...
随机推荐
- Cocos2d-x 3.0final 终结者系列教程03-源代码文件夹说明
话说今天从霍营到回龙观,走到天鑫家园东路.我肋哥去,堵死我啦.7:30出门,9:10还没到回龙观. 北京这交通真是坑爹.回过头想想.怪自己走小路,有时候确实快.可有时候真堵. 堵了35分钟后果断掉头, ...
- Codeforces 441C Valera and Tubes
题目链接:Codeforces 441C Valera and Tubes 没看到r >= 2一直错.让前几个管子占用2个格子.最后一个把剩下的都占用了.假设问题有解.这样做一定有解.其它策略就 ...
- 中间件监控之Apache
补 系统架构 nginx接到请求后把请求转发到tomcat,还有种方式是转发到apache(php项目),或者其他语言的应用服务器(放置我们的项目) ngnix:是web服务器,接受和转发请求用的,不 ...
- Spring Boot干货系列:(四)Thymeleaf篇
Spring Boot干货系列:(四)Thymeleaf篇 原创 2017-04-05 嘟嘟MD 嘟爷java超神学堂 前言 Web开发是我们平时开发中至关重要的,这里就来介绍一下Spring Boo ...
- JDBC事务和JTA (XA)事务区别
JDBC 事务 JDBC 事务是用 Connection 对象控制的.JDBC Connection 接口( java.sql.Connection )提供了两种事务模式:自动提交和手工提交. 在jd ...
- 手风琴式焦点图jQuery特效
手风琴式焦点图jQuery特效是一款鼠标点击人物图像滑动切换案例说明信息代码.效果图如下: 在线预览 源码下载 实现的代码. html代码: <div class="ag-cont ...
- linux命令--vi,vim
进入vi的命令 vi filename :打开或新建文件,并将光标置于第一行首 vi +n filename :打开文件.并将光标置于第n行首 vi + filename :打开文件.并将光标置 ...
- C语言 · 上帝造题五分钟
算法提高 上帝造题五分钟 时间限制:1.0s 内存限制:256.0MB 问题描述 第一分钟,上帝说:要有题.于是就有了L,Y,M,C 第二分钟,LYC说:要有向量.于是就有了长度为n ...
- uboot在nandflash存储时内存和NandFlash存储空间
硬件采用nandflash,nandflash为8位数据宽度,没有dataflash和norflash. Nandflash空间分配为 bootstrap + u-boot + env + linux ...
- Jquery解析json数组字符串
最近在工作中用到了Jquery来解析json字符串,网上解析jquery解析json单个对象的实例不少,但是jquery解析json数组的实例却是不多,下面我举一个简单的例子来跟大家分享与一下,本人水 ...