转换与继承
- 派生类对象也是基类对象。这意味着在使用基类的地方可以用派生类来替换。
派生类到基类的转换
- 当派生类以public方式继承基类时,编译器可自动执行的转换(向上转型upcasting安全转换)
- 派生类对象指针自动转换为基类对象指针
- 派生类对象引用自动转换为基类对象引用
- 派生类对象自动转换为基类对象(特有的成员消失)
- 当派生类以private/protected方式继承基类时
- 派生类对象指针(引用)转化为基类对象指针(引用)需要强制类型转换。但不能用static_cast,要用reinterpret_cast
- 不能把派生类对象强制转换为基类对象
回顾:
static_case 用于编译认可的静态转换。比如说从char到int,从double到int。或者具有转换构造函数。或者重载了类型转换运算符。
reinterpret_case 用于编译器不认可的静态转换,比如从int*转为int,在转型的过程中,不做任何对齐
const_cast 去除常量型
dynamic_cast 用于动态转换,安全的向下转型。多态
#include <iostream>
#include <string>
using namespace std;
class Employee
{
public:
Employee(const string& name,const int age, const int deptno):name_(name),age_(age),deptno_(deptno)
{
}
private:
string name_;
int age_;
int deptno_;
};
class Manager:public Employee
{
public:
Manager(const string& name,const int age, const int deptno,int level):Employee(name,age,deptno),level_(level)
{
}
private:
int level_;
};
class Manager2:private Employee
{
public:
Manager2(const string& name,const int age, const int deptno,int level):Employee(name,age,deptno),level_(level)
{
}
private:
int level_;
};
int main()
{
Employee el("zhangsna",25,20);
Manager ml("lsi",38,20,10);
Manager2 m2("laowang",40,15,8);
Employee* pe;
Manager* pm;
Manager2* pm2;
pe = ⪙
pm = &ml;
pm2 = &m2;
pe = &ml;//派生类指针可以转换为基类指针。将派生类对象看成基类对象
//pm = & pe; //基类指针无法转换为派生类指针,无法将基类对象看成派生类对象
el = ml;//派生类对象可以转换为基类对象,将派生类对象看成基类对象
//会产生对象切割,(派生类持有成员消失) 。object slicing
//pe = pm2;//私有或保护继承的时候,派生类对象指针不可以自动转换为基类对象指针
pe = (Employee*)(pm2);
pe = reinterpret_cast<Employee*>(pm2);
//el = m2;//私有保护继承的时候,派生类对象无法转换为基类对象
//el = reinterpret_cast<Employee>(m2)//即使是强制转换也不允许
}
基类到派生类的转换
- 基类对象指针(引用)可用强制类型转换为派生类对象指针(引用),而基类对象无法执行这类转换。
- 向下转型不安全,没有自动转换机制。
#include <iostream>
#include <string>
using namespace std;
class Employee
{
public:
Employee(const string& name,const int age, const int deptno):name_(name),age_(age),deptno_(deptno)
{
}
private:
string name_;
int age_;
int deptno_;
};
class Manager:public Employee
{
public:
Manager(const string& name,const int age, const int deptno,int level):Employee(name,age,deptno),level_(level)
{
}
private:
int level_;
};
class Manager2:private Employee
{
public:
Manager2(const string& name,const int age, const int deptno,int level):Employee(name,age,deptno),level_(level)
{
}
private:
int level_;
};
int main()
{
Employee el("zhangsna",25,20);
Manager ml("lsi",38,20,10);
Manager2 m2("laowang",40,15,8);
Employee* pe;
Manager* pm;
Manager2* pm2;
pe = ⪙
pm = &ml;
pm2 = &m2;
pe = &ml;//派生类指针可以转换为基类指针。将派生类对象看成基类对象
//pm = & pe; //基类指针无法转换为派生类指针,无法将基类对象看成派生类对象
el = ml;//派生类对象可以转换为基类对象,将派生类对象看成基类对象
//会产生对象切割,(派生类持有成员消失) 。object slicing
//pe = pm2;//私有或保护继承的时候,派生类对象指针不可以自动转换为基类对象指针
pe = (Employee*)(pm2);
pe = reinterpret_cast<Employee*>(pm2);
//el = m2;//私有保护继承的时候,派生类对象无法转换为基类对象
//el = reinterpret_cast<Employee>(m2)//即使是强制转换也不允许
//-------------------------------------------------------------------------------------------
pm = static_cast<Manager*>(pe);//基类指针可以强制转换为派生类指针,但是不安全
//m1 = static_cast<Manager>(el);
//m1 = reinterpret_cast<Manager>(el);//基类对象无法强制转换为派生类对象
}
转化构造函数
将其他转换为类类型
类型转换运算符重载
将类类型转换为其他类型
多重继承
- 单重继承——一个派生类最多只能有一个基类
- 多重继承——一个派生类可以有多个基类
- class 类名:继承方式 基类1,继承方式 基类2,…{};
- 派生类同时继承多个基类的成员,更好的软件重用
- 可能会有大量的二义性,多个基类中可能包含同名变量或函数
- 多重继承中解决访问歧义的方法:基类名::数据成员名(或成员函数(参数表))
- 明确指明要访问定义于那个基类中的成员
#include <iostream>
using namespace std;
class Bed
{
public:
Bed(int weight): weight_(weight)
{
}
void Sleep()
{
cout<<"Sleep..."<<endl;
}
int weight_;
};
class Sofa
{
public:
Sofa(int weight):weight_(weight)
{
}
void WatchTV()
{
cout<<"Watch Tv...."<<endl;
}
int weight_;
};
class SofaBed : public Bed,public Sofa
{
public:
SofaBed():Bed(0),Sofa(0)
{
FoldIn();
}
void FoldOut()
{
cout<<"FoldOut..."<<endl;
}
void FoldIn()
{
cout<<"FoldIn..."<<endl;
}
};
int main()
{
SofaBed sofabed;
//sofabed.weight_ = 10;
//sofabed.weight_ = 20;
sofabed.Bed::weight_ = 10;
sofabed.Sofa::weight_ = 20;
sofabed.WatchTV();
sofabed.FoldOut();
sofabed.Sleep();
}
虚继承与虚基类
- 当派生类从多个基类派生,而这些基类又从同一个基类派生,则在访问此共同基类中的成员时,将产生二义性——采用需基类来解决
- 虚基类的引入
- 用于有共同基类的场合
- 声明
- 以virtual修饰说明基类
- 例:class B1: virtual public BB
- 以virtual修饰说明基类
- 作用
- 主要用来解决多继承时可能发生的对同一基类继承多次产生的二义性问题
- 为最远的派生类提供唯一的基类成员,而不重复产生多次拷贝
在多重继承的代码中,沙发床只有一个重量,这样子重量重复,如何改进?改进版:
#include <iostream>
using namespace std;
class Furniture
{
public:
int weight_;
};
class Bed :virtual public Furniture
{
public:
void Sleep()
{
cout<<"Sleep..."<<endl;
}
int weight_;
};
class Sofa :virtual public Furniture
{
public:
void WatchTV()
{
cout<<"Watch Tv...."<<endl;
}
};
class SofaBed : public Bed,public Sofa
{
public:
void FoldOut()
{
cout<<"FoldOut..."<<endl;
}
void FoldIn()
{
cout<<"FoldIn..."<<endl;
}
};
int main()
{
SofaBed sofabed;
sofabed.weight_ = 10;
sofabed.WatchTV();
sofabed.FoldOut();
sofabed.Sleep();
}
虚基类及其派生类构造函数
- 虚基类的成员是由最远派生类的构造函数通过调用虚基类的构造函数进行初始化的。
- 在整个继承结构中,直接或间接继承虚基类的所有派生类,都必须在构造函数的成员初始化表中给出对虚基类的构造函数的调用。如果未列出,则表示调用该虚基类的默认构造函数。
- 在建立对象时,只有最远派生类的构造函数调用虚基类的构造函数,该派生类的其他基类对虚基类构造函数的调用被忽略。
#include <iostream>
using namespace std;
class Furniture
{
public:
Furniture(int weight):weight_(weight)
{
cout<<"Furniture...."<<endl;
}
~Furniture()
{
cout<<"~Furniture...."<<endl;
}
int weight_;
};
class Bed :virtual public Furniture
{
public:
Bed(int weight):Furniture(weight)
{
cout<<"Bed...."<<endl;
}
~Bed()
{
cout<<"~Bed...."<<endl;
}
void Sleep()
{
cout<<"Sleep..."<<endl;
}
int weight_;
};
class Sofa :virtual public Furniture
{
public:
Sofa(int weight):Furniture(weight)
{
cout<<"Sofa...."<<endl;
}
~Sofa()
{
cout<<"~Sofa...."<<endl;
}
void WatchTV()
{
cout<<"Watch Tv...."<<endl;
}
};
class SofaBed : public Bed,public Sofa
{
public:
SofaBed(int weight): Bed(weight),Sofa(weight),Furniture(weight)
{
cout<<"SofaBed...."<<endl;
}
~SofaBed()
{
cout<<"~SofaBed...."<<endl;
}
void FoldOut()
{
cout<<"FoldOut..."<<endl;
}
void FoldIn()
{
cout<<"FoldIn..."<<endl;
}
};
int main()
{
SofaBed sofabed(5);
sofabed.weight_ = 10;
sofabed.WatchTV();
sofabed.FoldOut();
sofabed.Sleep();
}
回顾类/对象大小计算
- 类大小计算遵循前面学过的结构体对齐原则
- 类的大小与数据成员有关与成员函数无关
- 类的大小与静态数据成员无关
- 虚继承对类的大小的影响
- 虚函数对类的大小的影响
虚继承对C++对象内存模型造成的影响
虚基类表
- virtual base table
- 本类地址与虚基类表指针地址的差
- 虚基类地址与虚基类表指针地址的差
- virtual base table pointer(vbptr)
#include <iostream>
using namespace std;
class BB
{
public:
int bb_;
};
class B1 : virtual public BB
{
public:
int b1_;
};
class B2 : virtual public BB
{
public:
int b2_;
};
class DD:public B1,public B2
{
public:
int dd_;
};
int main()
{
cout<< sizeof(BB)<<endl;
cout<< sizeof(B1)<<endl;
cout<< sizeof(DD)<<endl;
B1 b1;
long** p;
cout<<&b1<<endl;
cout<<&b1.bb_<<endl;
cout<<&b1.b1_<<endl;
p = (long**)&b1;
cout<<p[0][0]<<endl;
cout<<p[0][1]<<endl;
DD dd;
cout << &dd<<endl;
cout << &dd.bb_<<endl;
cout << &dd.b1_<<endl;
cout << &dd.b2_<<endl;
cout << &dd.dd_<<endl;
p = (long**)ⅆ
cout<<p[0][0]<<endl;
cout<<p[0][1]<<endl;
cout<<p[2][0]<<endl;
cout<<p[2][1]<<endl;
}