- 不带虚函数的单虚继承
#include "stdafx.h"
#include "iostream"
using namespace std;
class Base
{
public:
Base (int a = 1):base(a){}
void fun0(){cout << base << endl;}
int base;
};
class baseClass_01 : virtual public Base
{
public:
baseClass_01 (int a = 1) : base01(a){}
void fun0(){cout << base01 << endl;}
int base01;
};
int _tmain(int argc, _TCHAR* argv[])
{
baseClass_01 a(10);
return 0;
}
新增虚基类指针,指向虚基类表,虚基类表中首项存储虚基类指针的偏移量,
接下来依次存储虚基类的偏移量(偏移量是相对于虚基类表指针的存储地址)。
虚继承时,基类位于派生类的后面。普通继承基类在前。
- 不带虚函数的多虚继承
#include "stdafx.h"
#include "iostream"
using namespace std;
class baseClass_01
{
public:
baseClass_01 (int a = 1) : base01(a){}
void fun0(){cout << base01 << endl;}
int base01;
};
class baseClass_02
{
public:
baseClass_02 (int a = 3):base02(a){}
void fun2(){cout << base02 << endl;}
int base02;
};
class inheritClass : virtual public baseClass_01, virtual public baseClass_02
{
public:
inheritClass (int a = 2):derive(a){}
void fun1(){cout << derive << endl;}
int derive;
};
int _tmain(int argc, _TCHAR* argv[])
{
inheritClass a(10);
//Base *b = &a; 存在二义性,无法准确确定基类的地址
baseClass_01 *pb01 = &a;
baseClass_02 *pb02 = &a;
inheritClass* p1 = &a;
cout << " p1 = " << p1 << "\t pb01 = " << pb01 << "\t pb02 = " << pb02 << endl;
return 0;
}
9. 不带虚函数的菱形虚继承-1
#include "stdafx.h"
#include "iostream"
using namespace std;
class Base
{
public:
Base (int a = 1):base(a){}
int base;
};
class baseClass_01 : virtual public Base
{
private:
int i;
char c;
};
class baseClass_02 : virtual public Base
{
private:
int i;
char c;
};
class inheritClass :virtual public baseClass_01,virtual public baseClass_02
{
private:
char c;
};
int _tmain()
{
inheritClass a;
baseClass_01 *b01 = &a;
baseClass_02 *b02 = &a;
cout << " &a = " << &a << "\n b01 = " << b01 << "\n b02 = " << b02 << endl;
return 0;
}
派生类的内存分布时,也是由上到下分析。虚继承将基类置于内存末尾,但是置于末尾的顺序也有一定的次序。
首先Base先放到末尾,然后Base1放到末尾,最后Base2放到末尾。
10. 不带虚函数的菱形虚继承-2
#include "stdafx.h"
#include "iostream"
using namespace std;
class Base
{
public:
Base (int a = 1):base(a){}
int base;
};
class baseClass_01 : virtual public Base
{
private:
int i;
char c;
};
class baseClass_02 : virtual public Base
{
private:
int i;
char c;
};
class inheritClass : public baseClass_01, public baseClass_02
{
private:
char c;
};
int _tmain()
{
inheritClass a;
baseClass_01 *b01 = &a;
baseClass_02 *b02 = &a;
cout << " &a = " << &a << "\n b01 = " << b01 << "\n b02 = " << b02 << endl;
return 0;
}
- 带虚函数的单虚继承
#include "stdafx.h"
#include "iostream"
using namespace std;
class Base
{
public:
Base (int a = 1):base(a){}
virtual void fun0(){cout << base << endl;}
virtual void virt_fun1() {cout << "Base virt_fun1" << endl;}
int base;
};
class baseClass_01 :virtual public Base
{
private:
int i;
char c;
public:
virtual void virt_fun1(){ cout << "baseClass_01 virt_fun1" << endl; }
virtual void virt_fun2(){ cout << "baseClass_01 virt_fun2" << endl; }
void comm_fun1(){ cout << "baseClass_01 comm_fun1" << endl; }
void comm_fun2(){ cout << "baseClass_01 comm_fun2" << endl; }
};
int _tmain()
{
baseClass_01 a;
a.virt_fun1();
cout << "------------------------------" << endl;
baseClass_01 *b01 = &a;
b01->virt_fun1();
b01->virt_fun2();
b01->comm_fun1();
cout << "------------------------------" << endl;
return 0;
}
与普通的包含虚函数的单继承相比,派生类拥有自己的虚函数表以及虚函数表指针,而不是与基类共用一个虚函数表。
注意虚函数表指针和虚基类表指针的存储顺序。
12. 带虚函数的多虚继承
#include "stdafx.h"
#include "iostream"
using namespace std;
class baseClass_01
{
private:
int i;
char c;
public:
virtual void virt_fun1(){ cout << "baseClass_01 virt_fun1" << endl; }
virtual void virt_fun2(){ cout << "baseClass_01 virt_fun2" << endl; }
void comm_fun1(){ cout << "baseClass_01 comm_fun1" << endl; }
void comm_fun2(){ cout << "baseClass_01 comm_fun2" << endl; }
};
class baseClass_02
{
private:
int i;
char c;
public:
virtual void virt_fun2(){ cout << "baseClass_02 virt_fun2" << endl; }
virtual void virt_fun3(){ cout << "baseClass_02 virt_fun3" << endl; }
void comm_fun2(){ cout << "baseClass_02 comm_fun2" << endl; }
void comm_fun3(){ cout << "baseClass_02 comm_fun3" << endl; }
};
class inheritClass :virtual public baseClass_01,virtual public baseClass_02
{
private:
char c;
public:
virtual void virt_fun2(){ cout << "inheritClass virt_fun2" << endl; }
virtual void virt_fun3(){ cout << "inheritClass virt_fun3" << endl; }
virtual void virt_fun4(){ cout << "inheritClass virt_fun4" << endl; }
void comm_fun2(){ cout << "inheritClass comm_fun2" << endl; }
void comm_fun3(){ cout << "inheritClass comm_fun3" << endl; }
};
int _tmain()
{
inheritClass a;
a.virt_fun1();
cout << "------------------------------" << endl;
baseClass_01 *b01 = &a;
b01->virt_fun1();
b01->virt_fun2();
b01->comm_fun1();
cout << "------------------------------" << endl;
baseClass_02 *b02 = &a;
b02->virt_fun2();
b02->virt_fun3();
b02->comm_fun2();
cout << "------------------------------" << endl;
inheritClass *c = &a;
c->virt_fun1();
c->comm_fun1();
c->comm_fun2();
c->comm_fun3();
cout << "------------------------------" << endl;
cout << " &a = " << &a << "\n b01 = " << b01 << "\n b02 = " << b02 << "\n c = " << c << endl;
return 0;
}
13. 带虚函数的菱形虚继承-1
#include "stdafx.h"
#include "iostream"
using namespace std;
class Base
{
public:
Base (int a = 1):base(a){}
virtual void fun0(){cout << base << endl;}
int base;
};
class baseClass_01 :virtual public Base
{
private:
int i;
char c;
public:
virtual void virt_fun1(){ cout << "baseClass_01 virt_fun1" << endl; }
virtual void virt_fun2(){ cout << "baseClass_01 virt_fun2" << endl; }
void comm_fun1(){ cout << "baseClass_01 comm_fun1" << endl; }
void comm_fun2(){ cout << "baseClass_01 comm_fun2" << endl; }
};
class baseClass_02 :virtual public Base
{
private:
int i;
char c;
public:
virtual void virt_fun2(){ cout << "baseClass_02 virt_fun2" << endl; }
virtual void virt_fun3(){ cout << "baseClass_02 virt_fun3" << endl; }
void comm_fun2(){ cout << "baseClass_02 comm_fun2" << endl; }
void comm_fun3(){ cout << "baseClass_02 comm_fun3" << endl; }
};
class inheritClass : public baseClass_01, public baseClass_02
{
private:
char c;
public:
virtual void virt_fun2(){ cout << "inheritClass virt_fun2" << endl; }
virtual void virt_fun3(){ cout << "inheritClass virt_fun3" << endl; }
virtual void virt_fun4(){ cout << "inheritClass virt_fun4" << endl; }
void comm_fun2(){ cout << "inheritClass comm_fun2" << endl; }
void comm_fun3(){ cout << "inheritClass comm_fun3" << endl; }
};
int _tmain()
{
inheritClass a;
a.virt_fun1();
cout << "------------------------------" << endl;
baseClass_01 *b01 = &a;
b01->virt_fun1();
b01->virt_fun2();
b01->comm_fun1();
cout << "------------------------------" << endl;
baseClass_02 *b02 = &a;
b02->virt_fun2();
b02->virt_fun3();
b02->comm_fun2();
cout << "------------------------------" << endl;
inheritClass *c = (inheritClass*)b01;
c->virt_fun1();
c->comm_fun1();
c->comm_fun2();
c->comm_fun3();
cout << "------------------------------" << endl;
cout << " &a = " << &a << "\n b01 = " << b01 << "\n b02 = " << b02 << "\n c = " << c << endl;
return 0;
}
- 带虚函数的菱形虚继承-2
#include "stdafx.h"
#include "iostream"
using namespace std;
class Base
{
public:
Base (int a = 1):base(a){}
virtual void fun0(){cout << base << endl;}
int base;
};
class baseClass_01 :virtual public Base
{
private:
int i;
char c;
public:
virtual void virt_fun1(){ cout << "baseClass_01 virt_fun1" << endl; }
virtual void virt_fun2(){ cout << "baseClass_01 virt_fun2" << endl; }
void comm_fun1(){ cout << "baseClass_01 comm_fun1" << endl; }
void comm_fun2(){ cout << "baseClass_01 comm_fun2" << endl; }
};
class baseClass_02 :virtual public Base
{
private:
int i;
char c;
public:
virtual void virt_fun2(){ cout << "baseClass_02 virt_fun2" << endl; }
virtual void virt_fun3(){ cout << "baseClass_02 virt_fun3" << endl; }
void comm_fun2(){ cout << "baseClass_02 comm_fun2" << endl; }
void comm_fun3(){ cout << "baseClass_02 comm_fun3" << endl; }
};
class inheritClass :virtual public baseClass_01,virtual public baseClass_02
{
private:
char c;
public:
virtual void virt_fun2(){ cout << "inheritClass virt_fun2" << endl; }
virtual void virt_fun3(){ cout << "inheritClass virt_fun3" << endl; }
virtual void virt_fun4(){ cout << "inheritClass virt_fun4" << endl; }
void comm_fun2(){ cout << "inheritClass comm_fun2" << endl; }
void comm_fun3(){ cout << "inheritClass comm_fun3" << endl; }
};
int _tmain()
{
inheritClass a;
a.virt_fun1();
cout << "------------------------------" << endl;
baseClass_01 *b01 = &a;
b01->virt_fun1();
b01->virt_fun2();
b01->comm_fun1();
cout << "------------------------------" << endl;
baseClass_02 *b02 = &a;
b02->virt_fun2();
b02->virt_fun3();
b02->comm_fun2();
cout << "------------------------------" << endl;
inheritClass *c = &a;
c->virt_fun1();
c->comm_fun1();
c->comm_fun2();
c->comm_fun3();
cout << "------------------------------" << endl;
cout << " &a = " << &a << "\n b01 = " << b01 << "\n b02 = " << b02 << "\n c = " << c << endl;
return 0;
}