带Body的纯虚函数与不带Body的虚函数?(C++学习)
来源:互联网 发布:it技术学院 编辑:程序博客网 时间:2024/06/04 19:40
虚函数 纯虚函数 基类本身可生成对象 基类本身不能生成对象 基类中必须提供实现 必须在派生类中提供实现 "实体继承" "接口继承" 有一点有意思: 纯虚函数也可以提供实现 首先想到的:如果纯虚函数有了实现,是不是说明了这个基类可以直接生成对象了? 事实证明不行。g++与cl分别给出错误信息如下: 想想也是哈,如果这样的话,和虚函数就没有任何区别了。 然后闪现的是:是不是派生类可以不用为它提供实现了呢? 恩,出现和上面一样的编译错误。那么这个东西是怎么用的呢? 原来需要这样被调用。 为纯虚函数添加定义体应该没什么必要,似乎对纯析构函数,某些时候有用? 纯虚函数可以有函数体,那么虚函数是不是可以没有函数体呢? 而且今天在newsmth看到一个网友问:为什么没有函数体不出错? 我也不知道具体原因,只是感觉上:程序中根本就没使用它,应该不会出错。 甚至还在想,反正C的对象不需要使用A和B中的虚函数,这时创建C的对象应该也没问题吧? 编译器直接告诉结果了: 好奇怪啊?为何纯虚函数就不需要,虚函数非要有定义呢(我们又不需要使用这个虚函数)。 看来需要学习一点C++对象的内存模型了 http://stackoverflow.com/questions/1306778/c-virtual-pure-virtual-explained http://stackoverflow.com/questions/5481941/c-pure-virtual-function-have-body http://www.codeguru.com/forum/showthread.php?t=356281 http://www.parashift.com/c++-faq-lite/virtual-functions.html http://www.artima.com/cppsource/pure_virtual.html纯虚函数
class A{public: virtual void fun()=0;};void A::fun(){ printf("A::fun/n");};int main(){ A * a = new A; a->fun(); delete a; return 0;}
error: cannot allocate an object of abstract type 'A' because the following virtual functions are pure within 'A': virtual void A::fun()
error C2259: 'A' : cannot instantiate abstract class due to following members: 'void A::fun(void)' : is abstract see declaration of 'A::fun'
class B: public A{};int main(){ A * a = new B; a->fun(); delete a; return 0;}
class B: public A{public: void fun() { A::fun(); printf("B::fun/n"); }};
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.
ISO C++ 10.4 (2):A pure virtual function need be defined only if explicitly called with the qualified id syntax (5.1).
虚函数
class A{public: virtual void fun();};class B{public: virtual void fun();};class C: public A, public B{public: void fun() { printf("C::fun/n"); }};int main(){ return 0;}
int main(int argc, char* argv[]){ C c; c.fun(); return 0;}
cckOIw8K.o:virtual.cpp:(.text$_ZN1AC2Ev[A::A()]+0x8): undefined reference to `vtable for A'cckOIw8K.o:virtual.cpp:(.text$_ZN1BC2Ev[B::B()]+0x8): undefined reference to `vtable for B'collect2: ld returned 1 exit status
virtual.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall A::fun(void)" (?fun@A@@UAEXXZ)virtual.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall B::fun(void)" (?fun@B@@UAEXXZ)virtual.exe : fatal error LNK1120: 2 unresolved externals
参考
- 带Body的纯虚函数与不带Body的虚函数?(C++学习)
- 带默认实现的纯虚函数
- 带缓存与不带缓存函数的区别
- JS函数中带与不带括号的区别
- scala函数带=与不带=的区别
- 一个简单的带Body的自定义Tag开发过程
- 一个简单的带Body的自定义Tag开发过程
- 学习笔记:js里调用函数时,函数名带括号与不带括号的区别
- JSP自定义标签(3):带标签体(body)的标签
- JSP自定义标签:带标签体(body)的标签
- 自定义标签之 带Body的标签库
- 使用HttpClient发送带body的GET请求
- C/C++ 不带参数的回调函数 与 带参数的回调函数 函数指针数组 例子
- 同名函数的 带 params 参数 与 不带 params 参数的执行优先级
- <body></ body >里的属性
- 虚函数与纯函数的区别
- <body> 中的 JavaScript 函数
- html中body的事件onload定义的函数失效
- VC6XML
- 流
- 如何在对话框类中调用视图类中的函数
- 电子邮件成企业主动营销的首选工具--刘红杰老师
- 用jconsole监控tomcat的jvm的配置
- 带Body的纯虚函数与不带Body的虚函数?(C++学习)
- gcc - 用于linux系统下编程的编译器
- 正在做RDLC的Nested Data Region
- Const and volatile
- (转)VC 字节对齐
- CString 的效率
- 移植Mplayer到s3c2410上
- WINDBG本地调试WIN7需要注意的一些问题!
- git for windows 中文乱码