C++中的虚函数!
2013-12-12 12:07
246 查看
1 /* 2 * test.cpp 3 * 4 * Created on: 2013-4-19 5 * Author: Administrator 6 */ 7 #include <utils/RefBase.h> 8 #include <stdio.h> 9 namespace android { 10 11 class A: public RefBase { 12 13 }; 14 15 class B { 16 public: 17 virtual void p() { 18 printf("!!!!!!!!!!!A\n"); 19 } 20 }; 21 22 class C: public B { 23 public : 24 25 void p() 26 { 27 printf("!!!!!!!!!!!B\n"); 28 } 29 }; 30 extern "C" int main(int argc,char **argv) { 31 A* pA = new A; 32 { 33 sp<A> spA(pA); 34 wp<A> wpA(pA); 35 } 36 37 C c; 38 c.p(); 39 } 40 41 }
正常编译,运行结果:
1 root@android:/ # t 2 !!!!!!!!!!!B
第二种情况:
如果C中不实现p函数,及:
1 /* 2 * test.cpp 3 * 4 * Created on: 2013-4-19 5 * Author: Administrator 6 */ 7 #include <utils/RefBase.h> 8 #include <stdio.h> 9 namespace android { 10 11 class A: public RefBase { 12 13 }; 14 15 class B { 16 public: 17 virtual void p() { 18 printf("!!!!!!!!!!!A\n"); 19 } 20 }; 21 22 class C: public B { 23 public : 24 25 }; 26 extern "C" int main(int argc,char **argv) { 27 A* pA = new A; 28 { 29 sp<A> spA(pA); 30 wp<A> wpA(pA); 31 } 32 33 C c; 34 c.p(); 35 } 36 37 }
正常编译,正常运行。结果是:
1 root@android:/ # t 2 !!!!!!!!!!!A
第三种情况:
如果B中不实现p函数。编译报错:
1 #include <utils/RefBase.h> 2 #include <stdio.h> 3 namespace android { 4 5 class A: public RefBase { 6 7 }; 8 9 class B { 10 public: 11 virtual void p(); 12 }; 13 14 class C: public B { 15 public : 16 17 }; 18 extern "C" int main(int argc,char **argv) { 19 A* pA = new A; 20 { 21 sp<A> spA(pA); 22 wp<A> wpA(pA); 23 } 24 25 C c; 26 c.p(); 27 } 28 } 29 //报错: 30 **** Build of configuration Default for project T **** 31 32 E:\android\adt-bundle-windows-x86-20130219\android-ndk-r8e\ndk-build.cmd all 33 "Compile++ thumb : t <= test.cpp 34 Executable : t 35 E:/android/adt-bundle-windows-x86-20130219/android-ndk-r8e/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6/http://www.cnblogs.com/http://www.cnblogs.com/arm-linux-androideabi/bin/ld.exe: ./obj/local/armeabi/objs/t/test.o: in function main:jni/test.cpp:32: error: undefined reference to 'android::B::p()' 36 E:/android/adt-bundle-windows-x86-20130219/android-ndk-r8e/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6/http://www.cnblogs.com/http://www.cnblogs.com/arm-linux-androideabi/bin/ld.exe: ./obj/local/armeabi/objs/t/test.o: in function vtable for android::C:test.cpp(.data.rel.ro._ZTVN7android1CE+0x8): error: undefined reference to 'android::B::p()' 37 collect2: ld returned 1 exit status 38 make: *** [obj/local/armeabi/t] Error 1 39 40 **** Build Finished ****
第四种情况:
如果B中函数p为这样:
1 #include <utils/RefBase.h> 2 #include <stdio.h> 3 namespace android { 4 5 class A: public RefBase { 6 7 }; 8 9 class B { 10 public: 11 virtual void p() = 0; 12 }; 13 14 class C: public B { 15 public : 16 17 }; 18 extern "C" int main(int argc,char **argv) { 19 A* pA = new A; 20 { 21 sp<A> spA(pA); 22 wp<A> wpA(pA); 23 } 24 25 C c; 26 c.p(); 27 } 28 } 29 //报错信息: 30 **** Build of configuration Default for project T **** 31 32 E:\android\adt-bundle-windows-x86-20130219\android-ndk-r8e\ndk-build.cmd all 33 "Compile++ thumb : t <= test.cpp 34 jni/test.cpp: In function 'int android::main(int, char**)': 35 jni/test.cpp:31:4: error: cannot declare variable 'c' to be of abstract type 'android::C' 36 jni/test.cpp:20:7: note: because the following virtual functions are pure within 'android::C': 37 jni/test.cpp:17:15: note: virtual void android::B::p() 38 make: *** [obj/local/armeabi/objs/t/test.o] Error 1 39 40 **** Build Finished ****
说明:c++中的虚函数还是比较宽松的。只是没有想到C++中的基类虚函数居然可以自带实现。
解决 has virtual method 'p' but non-virtual destructor警告。
1 //给class B增加虚的析构函数,因为继承是从基类开始析构的。 2 //如果不添加会出现警告:Class '[C@3c9c92' has virtual method 'p' but non-virtual destructor 3 virtual ~B(){};
相关文章推荐
- C语言的模块仓库
- C/C++中优先级.结合方向与执行顺序的关系
- C++ 需要返回值的函数却没有返回值的情况 单例模式
- C++ 线程安全的单例模式
- C语言几个有意思的问题
- C++并发实战10:保护共享数据的可选机制
- C++拷贝构造函数(深拷贝,浅拷贝)
- C语言滤波程序
- C语言编写无类型的快速排序和归并排序
- C++ 多态与重载
- C++ 多态
- 神奇的C语言--C语言未定义行为一览
- C语言新标准C11
- C++中变量命名规则
- C++中多个关联文件编译的写法
- 很酷的C语言技巧
- C++必知必会之(31)协变返回类型
- Effective C++ 第二版 40)分层 41)继承和模板 42)私有继承
- 最小二乘法直线拟合(C++)
- C++ 字符集转换 codecvt utf-8 gb2312 url