欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c++-多态和vptr指针

程序员文章站 2022-08-02 22:38:24
多态的原理 验证vptr指针的存在 父类指针和子类指针的步长 ......

多态的原理

#define _crt_secure_no_warnings
#include <iostream>


using namespace std;

class parent
{
public:
    parent(int a) {
        this->a = a;
    }

    virtual void func(int a)
    {
        cout << "parent::func(int)..." << endl;
    }

    virtual void func(int a, int b, int c)
    {
        cout << "parent::func(int ,int ,int )...." << endl;
    }
private:
    int a;
};

class child :public parent
{
public:
    child(int a, int b) :parent(a)
    {
        this->b = b;
    }
    virtual void func(int a) 
    {
        cout << "child: func(int)..." << endl;
    }

    void func(int a, int b) {
        cout << "child :func(int ,int )..." << endl;
    }

    virtual void func(int a, int b, int c)
    {
        cout << "child ::func(int ,int ,int )..." << endl;
    }
private:
    int b;
};

void myfunc(parent *pp)
{
    pp->func(10);
}

int main(void)
{
    //parent *pp = new parent(10);
    //parent *cp = new child(100, 200);


    parent *pp = new child(100, 200);

    pp->func(10);//parent ? child
                    //如果调用一个普通函数,编译器根本就不会查找虚函数表。
                    //只有你调用的函数,是虚函数的时候,才会去查找虚函数表
    // myfunc(pp);
    pp->func(10, 20, 30);

    return 0;
}

验证vptr指针的存在

#define _crt_secure_no_warnings
#include <iostream>


using namespace std;

class parent
{
public:
    virtual void func()
    {
        cout << "parent::func().." << endl;
    }
    virtual void func(int a)
    {
        cout << "parent::func().." << endl;
    }
private:
    int a;
};


class parent2
{
public:
    void func()
    {
        cout << "parent2::func().." << endl;
    }
private:
    int a;
};
int main(void)
{
    parent p1;
    parent2 p2;


    cout << "sizeof(p1) " << sizeof(p1) << endl;//多出来的4个字节就是vptr指针所占用的空间。
    cout << "sizeof(p2) " << sizeof(p2) << endl;

    
    return 0;
}

vptr指针分布初始化

#define _crt_secure_no_warnings
#include <iostream>


using namespace std;

class parent
{
public:
    parent(int a)
    {
        cout << "parent(int ..)" << endl;
        this->a = a;

        //print();//是调用父类的print() 还是 子类的print()?
                //通过结果 此时调用的父类的print 并不是子类的print
    }

    virtual void print()
    {
        cout << "parent::print()...a = "<<a << endl;
    }
private:
    int a;
};

class child :public parent
{
public:
    child(int a, int b) :parent(a) //在调用父类的构造器的时候,会将vptr指针当做父类来处理。
                                    //此时会临时指向父类的虚函数表


    {
        //将子类对象的空间有编程子类对象处理,vptr指针就从指向父类的表 变成 指向子类的表

        cout << "child (int ,int )" << endl;
        this->b = b;
        print();//此时vptr指针已经回到了 子类的表, 调用的是子类的print函数。

    }

    virtual void print() {
        cout << "child ::print()..b = " << b << endl;
    }
private:
    int b;
};

int main(void)
{
    parent *pp = new child(10, 20);
    // pp->print();//发生多态

    delete pp;
    
    return 0;
}

父类指针和子类指针的步长

#define _crt_secure_no_warnings
#include <iostream>

using namespace std;


class parent
{
public:
    parent(int a)
    {
        this->a = a;
    }
    virtual void print()
    {
        cout << "parent::print a=  " << a << endl;
    }

    int a;
};

class child :public parent
{
public:
    child(int a) :parent(a)
    {

    }

    virtual void print()
    {
        cout << "child::print a=  " << a << endl;
    }

    int b;
};


int main(void)
{

    child array[] = { child(0), child(1), child(2) };
    
    parent *pp = &array[0];
    child *cp = &array[0];

    pp++;

    pp->print();
    cp->print();

#if 0
    pp++;//pp +sizeof(parent)
    cp++;//cp +sizeof(child)

    pp->print();
    cp->print();
#endif
    cout << "-----" << endl;

    int i = 0;
    for (cp= &array[0], i = 0; i < 3; i++, cp++) {
        cp->print();
    }

    return 0;
}