posts - 101,  comments - 57,  trackbacks - 0
1.多重继承

#include "iostream"
using namespace std;


class A
{
    
int a;
public:
    
virtual void Fun(int n)
    
{
        a 
= n;
        cout
<<"This is in A : "<<a<<endl;
    }

}
;

class B : public A
{
    
int b;
public:
    
virtual void Fun(int n)
    
{
        b 
= n;
        cout
<<"This is in B : "<<b<<endl;
    }

}
;

class C : public A
{
    
int c;
public:
    
virtual void Fun(int n)
    
{
        c 
= n;
        cout
<<"This is in C : "<<c<<endl;
    }

}
;

class D : public B, public C
{
    
int d;
public:
    
virtual void Fun(int n)
    
{
        d 
= n;
        cout
<<"This is in D : "<<d<<endl;
    }

}
;


int main()
{
    D d;
    d.Fun(
3);
    
return 0;
}


先来看看多重继承的对象组织的结构


实际上,多重继承vc都将它解释为这个结构。但是多重集成中实际在内存中的组织是很不一样的

00B606D8  0046F020  offset test3.D::`vftable'
00B606DC  CDCDCDCD    A::a
00B606E0  CDCDCDCD    B::b
00B606E4  0046F01C  offset test3.D::`vftable'
00B606E8  CDCDCDCD   A::a
00B606EC  CDCDCDCD  C::c
00B606F0  CDCDCDCD  D::d

note:
   按照道理来说两个offset test3.D::`vftable'应该指向同一个地方才对,可这里是?

   第二个offset test3.D::`vftable'指向的位置是第二篇讲过的跳转函数!

2.虚继承

#include "iostream"
using namespace std;


class A
{
    
int a;
public:
    
virtual void Fun(int n)
    
{
        a 
= n;
        cout
<<"This is in A : "<<a<<endl;
    }

}
;

class B : virtual  public  A
{
    
int b;
public:
    
virtual void Fun(int n)
    
{
        b 
= n;
        cout
<<"This is in B : "<<b<<endl;
    }

}
;

class C :virtual  public  A
{
    
int c;
public:
    
virtual void Fun(int n)
    
{
        c 
= n;
        cout
<<"This is in C : "<<c<<endl;
    }

}
;

class D : public  B, public  C
{
    
int d;
public:
    
virtual void Fun(int n)
    
{
        d 
= n;
        cout
<<"This is in D : "<<d<<endl;
    }

}
;


int main()
{
    D 
*pd = new D;
    pd
->Fun(sizeof(D));
    
return 0;
}


00A806D8  0046F02C  offset test3.D::`vbtable'
00A806DC  CDCDCDCD   B::b
00A806E0  0046F020  offset test3.D::`vbtable'
00A806E4  CDCDCDCD   C::c
00A806E8  CDCDCDCD   A::a
00A806EC  0046F01C  offset test3.D::`vftable'
00A806F0  CDCDCDCD  D::d

第一和第二个offset test3.D::`vbtable' 指向一个偏移跳转表,它的表中用偏移指向了真正的offset test3.D::`vbtable',第三个是真正的offset test3.D::`vbtable'地址

0046F01C >004011A9  test3.004011A9  第三个offset test3.D::`vbtable'
0046F020 >00000000                               第二个offset test3.D::`vbtable' 
0046F024  0000000C                               偏移C
0046F028  00000000
0046F02C >00000000                               第一个offset test3.D::`vbtable'
0046F030  00000014                                便宜14

 

posted on 2008-03-24 00:04 margin 阅读(473) 评论(0)  编辑 收藏 引用 所属分类: C/C++逆向工程

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿

随笔档案

文章分类

文章档案

收藏夹

常去的坛子

  • CVC电脑病毒论坛
  • 很多人说我是AV,我告诉他们:别瞧不起人,我们也能创造价值
  • 安全焦点
  • 黑客聚集的地方,一般是好酒最多的地方...
  • 看雪论坛
  • 国内最强的加密解密论坛,成醉其中经常夜不归宿
  • 驱动开发论坛
  • 厌倦了啤的朋友们,来我们来整点白的...痛痛快快的BSOD也好过隔鞋瘙痒!

我的朋友

  • Sen的blog
  • IDE方面资深的受害者...经常为一个变量的定义找不着北的痛苦程序员(深表同情)
  • 老罗的blog
  • 良师益友,千年水牛,引擎猛男,分析怪兽,墨镜酷哥,台球高手....

搜索

  •  

最新评论