/*
* main.cpp
*
* Created on: 2016年5月13日
* Author: root
*/
#include <iostream>
#include <string>
using namespace std;
typedef void(*fun)(void);
class A
{
public:
virtual void fun1()
{
cout << “a virtual fun1” << endl;
}
void fun2()
{
cout << “a fun2 ” << endl;
}
void fun3()
{
cout << “A fun 3 ” << endl;
}
~A()
{ cout << “destructor of A ” << endl; }
};
class B : public A
{
public:
void fun1()
{
cout << “b fun1” << endl;
}
virtual void fun2()
{
cout << “b virtual fun2″ << endl;
}
virtual void fun3()
{
cout << ” virtual fun3 of C ” << endl;
}
~B()
{
cout << “discruct of B ” << endl;
}
};
class C : public B
{
public:
virtual void fun1()
{
cout << ” C virtual fun1 ” << endl;
}
void fun2()
{
cout << ” C fun2 ” << endl;
}
void fun3()
{
cout << ” C fun3 ” << endl;
}
~C()
{
cout <<“destructor of c ” << endl;
}
};
class D: public C
{
public:
void fun1()
{
cout << “D fun1” << endl;
}
virtual void fun2()
{
cout << “D virtual fun2″ << endl;
}
void fun3()
{
cout << ” D fun3 ” << endl;
}
~D()
{
cout << “discruct of D” << endl;
}
};
int main()
{
B* x= new B();
x->A::fun1();
x->fun1();
x->fun2();
x->fun3();
delete x;
A* y= new C();
y->fun1();
y->fun2();
y->fun3();
delete y;
A* z= new B();
z->fun1();
z->fun2();
z->fun3();
delete z;
A* w= new D();
w->fun1();
w->fun2();
w->fun3();
delete w;
}
环境gcc4.9
centos6.5 i386
运行结果是
a virtual fun1
b fun1
b virtual fun2
virtual fun3 of C
discruct of B
destructor of A
C virtual fun1
a fun2
A fun 3
destructor of A
b fun1
a fun2
A fun 3
destructor of A
D fun1
a fun2
A fun 3
destructor of A
* main.cpp
*
* Created on: 2016年5月13日
* Author: root
*/
#include <iostream>
#include <string>
using namespace std;
typedef void(*fun)(void);
class A
{
public:
virtual void fun1()
{
cout << “a virtual fun1” << endl;
}
void fun2()
{
cout << “a fun2 ” << endl;
}
void fun3()
{
cout << “A fun 3 ” << endl;
}
~A()
{ cout << “destructor of A ” << endl; }
};
class B : public A
{
public:
void fun1()
{
cout << “b fun1” << endl;
}
virtual void fun2()
{
cout << “b virtual fun2″ << endl;
}
virtual void fun3()
{
cout << ” virtual fun3 of C ” << endl;
}
~B()
{
cout << “discruct of B ” << endl;
}
};
class C : public B
{
public:
virtual void fun1()
{
cout << ” C virtual fun1 ” << endl;
}
void fun2()
{
cout << ” C fun2 ” << endl;
}
void fun3()
{
cout << ” C fun3 ” << endl;
}
~C()
{
cout <<“destructor of c ” << endl;
}
};
class D: public C
{
public:
void fun1()
{
cout << “D fun1” << endl;
}
virtual void fun2()
{
cout << “D virtual fun2″ << endl;
}
void fun3()
{
cout << ” D fun3 ” << endl;
}
~D()
{
cout << “discruct of D” << endl;
}
};
int main()
{
B* x= new B();
x->A::fun1();
x->fun1();
x->fun2();
x->fun3();
delete x;
A* y= new C();
y->fun1();
y->fun2();
y->fun3();
delete y;
A* z= new B();
z->fun1();
z->fun2();
z->fun3();
delete z;
A* w= new D();
w->fun1();
w->fun2();
w->fun3();
delete w;
}
环境gcc4.9
centos6.5 i386
运行结果是
a virtual fun1
b fun1
b virtual fun2
virtual fun3 of C
discruct of B
destructor of A
C virtual fun1
a fun2
A fun 3
destructor of A
b fun1
a fun2
A fun 3
destructor of A
D fun1
a fun2
A fun 3
destructor of A
解决方案
100
w 是 A* ,在 A 里, fun2 不是虚函数。所以,这里直接调用 A 的 fun2 。
这有当 func 在 A 中是虚函数时,才会根据指针的动态类型确定具体调用哪一个函数。