虚函数和虚析构函数在调用处理上的不同
11 月.30, 2015 in
编程小记
虚函数和虚析构函数在调用的处理上是不同的,虚析构函数是子调用父,层层调用,对于普通的虚函数,则是重载的方式,需要注意。下面的代码执行结果是:
derivedA say: hello virtual function!.
~finalA
~derivedA
~baseA
#include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <errno.h> class baseA{ public: virtual ~baseA() { printf("~baseA\r\n"); }; virtual void sayHello(const char *str){ printf("baseA say: %s.\r\n", str); }; }; class derivedA : public baseA { public: virtual ~derivedA() { printf("~derivedA\r\n"); }; virtual void sayHello(const char *str){ printf("derivedA say: %s.\r\n", str); }; }; class finalA : public derivedA { public: ~finalA() { printf("~finalA\r\n"); }; void sayHello(const char *str){ printf("derivedA say: %s.\r\n", str); }; }; int main() { finalA *a = new finalA(); a->sayHello("hello virtual function!"); delete a; return 0; } |
Leave a Reply
You must be logged in to post a comment.