分类: C/C++
2008-08-01 16:59:49
class 类名{ public: virtual 成员函数说明; } class 类名:基类名{ public: virtual 成员函数说明; }三、 虚函数在内存中的结构
#include "iostream.h" #include "string.h" class A { public: virtual void fun0() { cout << "A::fun0" << endl; } }; int main(int argc, char* argv[]) { A a; cout << "Size of A = " << sizeof(a) << endl; return 0; }结果如下:Size of A = 4
class A { public: void fun0() { cout << "A::fun0" << endl; } }; int main(int argc, char* argv[]) { A a; cout << "Size of A = " << sizeof(a) << endl; return 0; }结果如下:Size of A = 1
class A { public: virtual void fun0() { cout << "A::fun0" << endl; } int a; int b; }; int main(int argc, char* argv[]) { A a; cout << "Size of A = " << sizeof(a) << endl; return 0; }结果如下:Size of A = 12
class A { public: virtual void fun0() { cout << "A::fun0" << endl; } virtual void fun1() { cout << "A::fun1" << endl; } int a; int b; }; int main(int argc, char* argv[]) { A a; cout << "Size of A = " << sizeof(a) << endl; return 0; }结果如下:结果如下:
class A { public: virtual void f() { } }; class B { public: virtual void f() { } }; class C { public: virtual void f() { } }; class Drive : public A, public B, public C { }; int main() { Drive d; cout << "Size is = " << sizeof(d) << endl; return 0; }结果如下:Size is = 12 ,相信大家一看下面的结构图就会很清楚,
class A { public: virtual void f() { cout << "A::f" << endl; } }; class B :public A{ public: virtual void f() { cout << "B::f" << endl;} }; class C :public A { public: virtual void f() { cout << "C::f" << endl;} }; class Drive : public C { public: virtual void f() { cout << "D::f" << endl;} }; int main(int argc, char* argv[]) { A a; B b; C c; Drive d; a.f(); b.f(); c.f(); d.f(); return 0; } 结果:A::f B::f C::f D::f不用解释,相信大家一看就明白什么道理!注意:多态不是函数重载
class A{ public: virtual void f(){cout << "A::f" << endl;}; }; class B:public A{ public: virtual void f(){cout << "B::f" << endl;}; }; class C:public A{ public: virtual void f(){cout << "C::f" << endl;}; }; void test(A *a){ a->f(); }; int main(int argc, char* argv[]) { B *b=new B; C *c=new C; char choice; do{ cout<<"type B for class B,C for class C:"<>choice; if(choice==''b'') test(b); else if(choice==''c'') test(c); }while(1); cout< 在上面的例子中,如果把类A,B,C中的virtual修饰符去掉,看看打印的结果,然后再看下面一个例子想想两者的联系。如果把B和C中的virtual修饰符去掉,又会怎样,结果和没有去掉一样。
7.在基类中调用继承类的函数(如果此函数是虚函数才能如此) 还是先看例子:class A { public: virtual void fun() { cout << "A::fun" << endl; } void show() { fun(); } }; class B : public A { public: virtual void fun() { cout << "B::fun" << endl; } }; int main() { A a; a.show(); return 0; }打印结果:A::fun
在6中的例子中,test(A *a)其实有一个继承类指针向基类指针隐式转化的过程。可以看出利用虚函数我们可以在基类调用继承类函数。但如果不是虚函数,继承类指针转化为基类指针后只可以调用基类函数。反之,如果基类指针向继承类指针转化的情况怎样,这只能进行显示转化,转化后的继承类指针可以调用基类和继承类指针。如下例子:class A { public: void fun() { cout << "A::fun" << endl; } }; class B : public A { public: void fun() { cout << "B::fun" << endl; } void fun0() { cout << "B::fun0" << endl; } }; int main() { A *a=new A; B *b=new B; A *pa; B *pb; pb=static_cast(a); //基类指针向继承类指针进行显示转化 pb->fun0(); pb->fun(); return 0; }参考资料:1.科学出版社 《C 程序设计》 2.Zeeshan Amjad 《ATL on the Hood》下载本文示例代码