飘云阁

 找回密码
 加入我们

QQ登录

只需一步,快速开始

查看: 2096|回复: 3

[C/C++] 虚表中的潜规则

[复制链接]

该用户从未签到

发表于 2010-1-28 00:47:25 | 显示全部楼层 |阅读模式
//////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////
class MyString  
{
public:
        char * m_str;
public:
        MyString(char * str);
        ~MyString();

};

MyString::MyString(char * str)
{
        m_str = new char(strlen(str) + sizeof(char ));
        strcpy(m_str,str);
}

MyString::~MyString()
{

}

//////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////
class MyPowerString : public MyString  
{
public:
        char * m_p;
public:
        MyPowerString(char * str);
        virtual ~MyPowerString();

};

MyPowerString::MyPowerString(char * str):MyString(str)
{
        m_p = new char(strlen(str) + sizeof(char ));
        strcpy(m_p,str);
}

MyPowerString::~MyPowerString()
{

}
//////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
        MyString * pstr = new MyPowerString("hello!");  // 潜规则一
        if (pstr)
        {
                //pstr = (MyString *)((int*)pstr - 1);
                delete (MyPowerString *)pstr;  // 潜规则二
                pstr = NULL;
        }
        return 0;
}
PYG19周年生日快乐!

该用户从未签到

 楼主| 发表于 2010-1-28 02:20:47 | 显示全部楼层
//////////////////////////////////////////////////////////////////////
// A
//////////////////////////////////////////////////////////////////////
class A  
{
public:
        A(int a);
        virtual ~A();
public:
        int m_a;
        int GetData();
};

A::A(int a)
{
        m_a = a;
}

A::~A()
{
        m_a = 0 ;
}

int A::GetData()
{
        return m_a;
}

//////////////////////////////////////////////////////////////////////
// B
//////////////////////////////////////////////////////////////////////
class B : public A  
{
public:
        B(int a,int b,int d);
        virtual ~B();
public:
        int m_b;
        D Objd;
        int GetData();
};

B::B(int a,int b,int d):Objd(d),A(a)
{
        m_b = b;
}

B::~B()
{
        m_b = 0;
}

int B::GetData()
{
        return m_b;
}

//////////////////////////////////////////////////////////////////////
// D
//////////////////////////////////////////////////////////////////////
class D  
{
public:
        D(int d);
        virtual ~D();
public:
        int m_d;
        int GetData();
};

D::D(int d)
{
        m_d = d;
}

D::~D()
{

}

int D::GetData()
{
        return m_d;
}

//////////////////////////////////////////////////////////////////////
// main()  第一种玩法 对象调用其他类的方法
//////////////////////////////////////////////////////////////////////

int main(int argc, char* argv[])
{
        B theb(10,20,30);
        cout<< theb.GetData() << endl;
        cout<< ((A)theb).GetData() << endl;
        cout<< ((D *)&theb)->GetData() << endl;     // 这个时候把 thea 的地址给D的成员函数玩
        cout<< ((D *)&theb.m_a)->GetData() << endl; // 这个时候把 theb 的地址给D的成员函数玩
        return 0;
}
PYG19周年生日快乐!

该用户从未签到

 楼主| 发表于 2010-1-28 02:52:38 | 显示全部楼层

BT 之 3

class Test
{
public:
        int m_ta;
public:
        int DataDataDec(int a)
        {
                return m_ta - a;
        }
        int DataDataDec()
        {
                return m_ta;
        }
};

class Func
{
public:
        int m_fa;
        int m_fb;
public:
        Func(int a,int b):m_fa(a),m_fb(b){}
        int GetDataSum()
        {
                return m_fa + m_fb;
        }
};

typedef int (Test::*FUNC_TEST)(int);
typedef int (Func::*FUNC_FUNC)(int);

int main(int argc, char* argv[])
{
        FUNC_TEST p = Test::DataDataDec;
       
        Func ObjC(10,20);
        cout<< ((Test * )&ObjC)->DataDataDec(5) <<endl;
        cout<< (ObjC.*(FUNC_FUNC)Test::DataDataDec)(3) << endl;
        cout<< (&ObjC->*(FUNC_FUNC)p)(2) << endl;s
        // 有this指针,有明确的函数地址,就可以实现调用
        // 至于数据将如何处理,调用者必须自己了解内存结构
        return 0;
}



啥成员函数不成员函数的 就是一段内存而已

其实内存中本没有什么类 扯淡的人多了 大家自己都累了
PYG19周年生日快乐!

该用户从未签到

发表于 2010-1-29 21:58:14 | 显示全部楼层
其实内存中本没有什么类 扯淡的人多了 大家自己都累了
这句话经典哈……
:funk: 也不知道这里支不支持小灌一下……
PYG19周年生日快乐!
您需要登录后才可以回帖 登录 | 加入我们

本版积分规则

快速回复 返回顶部 返回列表