c++引用与虚函数,模拟实现自己的虚函数表

#include <iostream>
using namespace std;

class Base
{
public:

    Base()
    {
        cout<<"Generate a Base"<<endl;
    }
    virtual ~Base()
    {	
        cout<<"Destroy a Base"<<endl;
    }


    virtual void reclaim()
    {
        cout<<"reclaim a Base"<<endl;
    }
};

class Derived:public Base
{
public:

    Derived():Base()
    {
        tmp = new int[10];
        cout<<"Generate a Derived"<<endl;
    }
    virtual ~Derived()
    {	
        delete []tmp;
        cout<<"Destroy a Derived"<<endl;
    }
    virtual void reclaim()
    {
        cout<<"reclaim a Derived"<<endl;
    }
private:
    int* tmp;
};




int main()
{
    {
        Derived d;
        Base& b = d;
        b.reclaim();
    }
    
    char c_exit;
    cin>>c_exit;
    
}

53行Base& b=d;

b与d代表同一个对象,拥有同一个虚函数表vtable。

如果改成Base b=d;

b与d不是同一个对象,而且拥有不同类型,拥有不同的虚函数表vtable。


模拟虚函数表,引用自mattew wilson <imperfect c++>;实际上vlc中,用C语言,通过类似的方法模拟了虚函数表。

//IObject.h

#include <assert.h>

# ifdef __cplusplus
extern "C"
{
#endif


struct IObjectVTable
{
    void (*SetName)(struct IObject *obj, char const *s);
    char const *(*GetName)(struct IObject const *obj);
};

struct IObject
{
    struct IObjectVTable *const vtable;

# ifdef __cplusplus
protected:
    typedef struct IObjectVTable    vtable_t;

protected:
    IObject(vtable_t *vt)
        : vtable(vt)
    {}
    IObject(IObject const &, vtable_t *vt)
        : vtable(vt)
    {}

public:
    inline void SetName(char const *s)
    {
        assert(NULL != vtable);

        vtable->SetName(this, s);
    }
    inline char const *GetName() const
    {
        assert(NULL != vtable);

        return vtable->GetName(this);
    }
private:
    IObject(IObject const &rhs);
    IObject &operator =(IObject const &rhs);
protected:
    ~IObject()
    {

    }
# endif /* __cplusplus */
};

IObject *make_Object(char const *s);
void release_Object(IObject*& obj);

# ifdef __cplusplus
};
#endif



//ObjectServer.cpp

#include <stdio.h>

#include "IObject.h"

class Object1
    : public IObject
{
public:
    typedef Object1     class_type;

public:
    virtual void SetName(char const *s)
    {
        m_name = s;
    }
    virtual char const *GetName() const
    {
        return m_name;
    }

public:
    Object1()
        : IObject(GetVTable())
    {}
    Object1(Object1 const &rhs)
        : IObject(GetVTable())
        , m_name(rhs.m_name)
    {}
public:
    ~Object1()
    {

    }

private:
    static void SetName_(IObject *this_, char const *s)
    {
        static_cast<class_type*>(this_)->SetName(s);
    }
    static char const *GetName_(IObject const *this_)
    {
        return static_cast<class_type const*>(this_)->GetName();
    }

    static vtable_t *GetVTable()
    {
        static vtable_t s_vt = MakeVTable();

        return &s_vt;
    }

    static vtable_t MakeVTable()
    {
        vtable_t vt = { SetName_, GetName_ };

        return vt;
    }


private:
    char const *m_name;
};

IObject *make_Object(char const *s)
{
    IObject *obj = new Object1();
    obj->SetName(s);
    return obj;
}
void release_Object(IObject*& obj)
{
    Object1* p_b = static_cast<Object1*>(obj);
    delete p_b;
    obj = NULL;
}

int main(int argc, char** argv)
{
    char const *strings[] = 
    {
        "Reginald Perrin"
        ,   "Archie Goodwin"
        ,   "Scott Tracy"
    };

    for(int i = 0; i < 3; ++i)
    {
        printf("Calling make_Object()\n");
        IObject *obj = make_Object(strings[i]);

        printf("%s\n", obj->GetName());
        release_Object(obj);
    }

    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中的多态性是通过虚函数实现的。在含有虚函数的类中,编译器会自动添加一个指向虚函数的指针,这个指针通常称为虚函数指针。虚函数是一个存储类的虚函数地址的数组,每个类有一个对应的虚函数。当一个类对象被创建时,会自动分配一个指向它的虚函数的指针。 虚函数指针的大小和虚函数的大小都与具体实现相关。在一般情况下,虚函数指针的大小为4或者8个字节,虚函数的大小取决于类中虚函数的个数。 以下是一个模拟实现: ```c++ #include <iostream> using namespace std; class A { public: virtual void func1() { cout << "A::func1" << endl; } virtual void func2() { cout << "A::func2" << endl; } }; class B : public A { public: virtual void func1() { cout << "B::func1" << endl; } }; int main() { A* a = new A(); B* b = new B(); cout << "size of A: " << sizeof(A) << endl; cout << "size of B: " << sizeof(B) << endl; cout << "size of a: " << sizeof(a) << endl; cout << "size of b: " << sizeof(b) << endl; a->func1(); a->func2(); b->func1(); b->func2(); delete a; delete b; return 0; } ``` 输出结果: ``` size of A: 8 size of B: 8 size of a: 8 size of b: 8 A::func1 A::func2 B::func1 A::func2 ``` 在上面的代码中,我们定义了两个类A和B,其中B继承自A。类A和B都含有虚函数,因此编译器会为它们添加虚函数指针。在main函数中,我们创建了一个A类对象和一个B类对象,并输出了它们的大小以及指针的大小。接着我们调用了每个对象的虚函数,可以看到B对象的func1()覆盖了A对象的func1(),而A对象的func2()没有被覆盖。最后我们删除了这两个对象,避免内存泄漏。 需要注意的是,虚函数指针的大小和虚函数的大小是不确定的,取决于具体实现。此外,虚函数指针通常被放在对象的开头,因此虚函数通常被放在内存中较靠前的位置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值