简介: CSDN博客专家,专注Android/Linux系统,分享多mic语音方案、音视频、编解码等技术,与大家一起成长!
优质专栏:Audio工程师进阶系列【原创干货持续更新中……】🚀
人生格言: 人生从来没有捷径,只有行动才是治疗恐惧和懒惰的唯一良药.

1.前言
本篇目的:理解C++之子类指向父类,父类指向子类总结
2.应用实例
v1.0 普通继承
#include <iostream>
using namespace std;
class GP{
  public:
  GP(){
    printf("xxx--------->%s(), line = %d\n",__FUNCTION__,__LINE__);
  }
  void test_gp(){
    printf("xxx--------->%s(), line = %d\n",__FUNCTION__,__LINE__);
  }
};
class Parent : public GP{
public:
  Parent(){
    printf("xxx--------->%s(), line = %d\n",__FUNCTION__,__LINE__);
  }
  void test_p(){
    printf("xxx--------->%s(), line = %d\n",__FUNCTION__,__LINE__);
  }
};
class Child : public Parent{
public:
  Child(){
    printf("xxx--------->%s(), line = %d\n",__FUNCTION__,__LINE__);
  }
  void test_c(){
    printf("xxx--------->%s(), line = %d\n",__FUNCTION__,__LINE__);
  }
};
int main(){
  //v1.0 父类指针对象转换为子类,子类可以访问继承的父类函数和变量.
  Child *c = static_cast<Child*>(new Parent);
  c->test_c();
  c->test_p();
  printf("\n");
  //v2.0 子类指针对象转换为父类,然而,父类只能访问父类的所有资源.
  Parent *p = static_cast<Parent*>(new Child);
  p->test_p();
  //p->test_c();//报错:Parent父类指针访问Child子类成员函数,越界报错.
  printf("\n");
  //v3.0 爷爷类GP指向孙子类Child
  GP *gp = static_cast<GP*>(new Child);
  gp->test_gp();
  printf("\n");
  //v4. 孙子类Child指向爷爷类GP
  Child *c1 = static_cast<Child*>(new GP);
  c1->test_gp();
  c1->test_p();
  c1->test_c();
}
普通继承总结
  1.子类指向父类或者爷爷类
    子类会继承他们的遗产,可以访问他们的public的所有资源.
  2.父类或爷爷类指向子类
    父类或爷爷类只能访问自己的资源,不能访问子类.
v2.0. 多态继承(纯虚函数)
#include <iostream>
using namespace std;
class GP{
  public:
  GP(){
    printf("xxx--------->%s(), line = %d\n",__FUNCTION__,__LINE__);
  }
  void test_gp(){
    printf("xxx--------->%s(), line = %d\n",__FUNCTION__,__LINE__);
  }
};
class Parent : public GP{
public:
  Parent(){
    printf("xxx--------->%s(), line = %d\n",__FUNCTION__,__LINE__);
  }
  void test_p(){
    printf("xxx--------->%s(), line = %d\n",__FUNCTION__,__LINE__);
  }
};
class Child : public Parent{
public:
  Child(){
    printf("xxx--------->%s(), line = %d\n",__FUNCTION__,__LINE__);
  }
  void test_c(){
    printf("xxx--------->%s(), line = %d\n",__FUNCTION__,__LINE__);
  }
};
int main(){
  //v1.0 父类指针对象转换为子类,子类可以访问继承的父类函数和变量.
  Child *c = static_cast<Child*>(new Parent);
  c->test_c();
  c->test_p();
  printf("\n");
  //v2.0 子类指针对象转换为父类,然而,父类只能访问父类的所有资源.
  Parent *p = static_cast<Parent*>(new Child);
  p->test_p();
  //p->test_c();//报错:Parent父类指针访问Child子类成员函数,越界报错.
  printf("\n");
  //v3.0 爷爷类GP指向孙子类Child
  GP *gp = static_cast<GP*>(new Child);
  gp->test_gp();
  printf("\n");
  //v4. 孙子类Child指向爷爷类GP
  Child *c1 = static_cast<Child*>(new GP);
  c1->test_gp();
  c1->test_p();
  c1->test_c();
}
多态继承(纯虚函数)总结
  1.子类指向父类或者爷爷类
    子类会继承他们的遗产,可以访问他们的public的所有资源.
  2.父类或爷爷类指向子类
    父类或爷爷类只能访问自己的资源,不能访问子类.
v3.0 复杂类继承
#include <iostream>
using namespace std;
class Refbase{
  public:
  Refbase(){
    printf("xxx--------->%s(), line = %d\n",__FUNCTION__,__LINE__);
  }
  //virtual void test_Rf() = 0;
};
class IBase : public Refbase{
public:
  IBase(){
    printf("xxx--------->%s(), line = %d\n",__FUNCTION__,__LINE__);
  }
  void test_Rf(){
    printf("xxx--------->%s(), line = %d\n",__FUNCTION__,__LINE__);
  }
};
class BpHwRefBase : public Refbase{
public:
  BpHwRefBase(){
    printf("xxx--------->%s(), line = %d\n",__FUNCTION__,__LINE__);
  }
  void test_Rf(){
    printf("xxx--------->%s(), line = %d\n",__FUNCTION__,__LINE__);
  }
};
template<typename INTERFACE>
class BpInterface : public BpHwRefBase
{
public:
  BpInterface(){
    printf("xxx--------->%s(), line = %d\n",__FUNCTION__,__LINE__);
  }
};
class BpHwBase: public BpInterface<BpHwBase>{
public:
  BpHwBase(){
    printf("xxx--------->%s(), line = %d\n",__FUNCTION__,__LINE__);
  }
};
int main(){
  //1.BpHwRefBase和IBase都继承自Refbase类.父类Refbase指向子类IBase.
  Refbase *rf = new IBase;
  //2.父类Refbase指向子类IBase的实例转换成孙子类BpHwBase
  BpHwBase* bpBase = static_cast<BpHwBase*>(rf);
  //or
  //BpHwRefBase对象指向孙子类BpHwBase对象,调用test_Rf()时,还是调用的BpHwRefBase类的函数.
  BpHwRefBase *bhr = static_cast<BpHwRefBase*>(bpBase);
  bhr->test_Rf();
  printf("\n");
}
总结
复杂类,下面结论依然成立。
1.基类指向子类,最终调用基类的成员函数.
2.子类指向基类,拥有基类一切的资源.



















