内容概要: 满足下面2个条件时, 1. 父类有虚函数,子类无虚函数(即无虚函数重写或无虚函数覆盖) 2. 非虚继承 子类对象之内存布局 前述相关内容参考: 1. http://blog.sina.com.cn/s/blog_5f5fff010100cv8j.html 2. http://blog.sina.com.cn/s/blog_5f5fff010100cv90.html~type=v5_one&label=rela_nextarticle 3. http://blog.sina.com.cn/s/blog_5f5fff010100cvd6.html~type=v5_one&label=rela_nextarticle Base类中有两个虚函数vfBase_1()、vfBase_2()和一个整形成员变量m_base, Derived类中有一个整形成员变量m_derived,二者的关系如下: 代码如下: #include <iostream> using namespace std; class Base { public: }; class Derived : public Base { public: }; typedef void (*VFun)(void); // 改为template形式,因为不能确定传进来的参数是Base类型的指针还是Derived类型的指针 template<typename T> VFun virtualFunctionPointer(T* b, int i) { } int main(void) { } 运行结果如下: Derived对象的memory layout图解如下: |
|
来自: astrotycoon > 《深度理解C 》