多继承类的指针偏移问题

例如:
struct C1
{
public:
virtual void D1() = 0;
};
class C2
{
public:
virtual void D2() = 0;
};
class C : public C1, public C2
{
public:
virtual void D1()
{
C* p = this;
C1* q = this;
C2* r = this;
}
virtual void D2()
{
C* p = this;
C1* q = this;
C2* r = this;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
C c;
c.D1();
c.D2();
return 0;
}
this指针会在转换时调整
D1中this: 0x0035f7fc     p: 0x0035f7fc     q: 0x0035f7fc     r: 0x0035f800
D2中this: 0x0035f800   p: 0x0035f7fc     q: 0x0035f7fc     r: 0x0035f800
发表评论?

0 条评论。

发表评论


注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>