问题描述
我在阅读这本书。在书中有一个例子:
I'm reading the book Inside the C++ Object Model. In the book there's an example like:
struct Base1
{
int v1;
};
struct Base2
{
int v2;
};
class Derived : public Base1, public Base2 {};
printf("&Derived::v1 = %p\n", &Derived::v1); // Print 0 in VS2008/VS2012
printf("&Derived::v2 = %p\n", &Derived::v2); // Print 0 in VS2008/VS2012
在上面的代码中,打印地址Derived :: v1 & Derived :: v2将都是 0 。但是,如果通过变量打印相同的地址:
In the previous code, the print of address Derived::v1 & Derived::v2 will both be 0. However, if print the same address via a variable:
int Derived::*p;
p = &Derived::v1;
printf("p = %p (&Derived::v1)\n", p); // Print 0 in VS2008/VS2012 as before
p = &Derived::v2;
printf("p = %p (&Derived::v2)\n", p); // Print 4 in VS2008/VS2012
通过检查& Derived :: v1和p
By examining the size of &Derived::v1 and p, I get 4 in both.
// Both are 4
printf("Size of (&Derived::v1) is %d\n", sizeof(&Derived::v1));
printf("Size of p is %d\n", sizeof(p));
Derived :: v1的地址为 0 ,但地址of Derived :: v2将为 4 。我不明白为什么& Derived :: v2在分配给变量时变为 4 。
The address of Derived::v1 will be 0, but the address of Derived::v2 will be 4. I don't understand why &Derived::v2 became 4 when assign it to a variable.
检查汇编代码,查询Derived :: v2的地址,它被翻译成 0 ;但是当它分配给一个变量时,它被转换为一个 4 。
Examine the assembly code, when directly query the address of Derived::v2, it is translated to a 0; but when assign it to a variable, it gets translated to a 4.
我在VS2008& VS2012,结果是一样的。所以我认为必须有一些理由让微软选择这样的设计。
I tested it on both VS2008 & VS2012, the result is the same. So I think there's must be some reason to make Microsoft choose such design.
如果你这样做:
d1.*(&Derived::v2) = 1;
显然& Derived :: v2 不是 0 。为什么编译器区分这两种情况?
Apparently &Derived::v2 is not 0. Why does the compiler distinguish this two cases?
任何人都可以告诉后面发生的事情吗?谢谢!
Can anyone please tell the thing happens behind? Thank you!
- 编辑 -
对于那些认为& Derived :: v1 doesn'得到有效的地址。
For those think the &Derived::v1 doesn't get a valid address. Haven't you ever did this?
Derived d1, d2;
d1.*p = 1;
d2.*p = 1;
推荐答案
海报问我这个,也怀疑类似的错误原因。这不是特定于VC ++。
The poster asked me about this, and at first I also suspected similar wrong causes. This is not specific to VC++.
事实证明发生的是,& Derived :: v2
不是 int Derived :: *
,但 int Base2 :: *
零,因为它是相对于Base2的偏移。当你显式地将它转换为 int Derived :: *
时,偏移量被更正。
It turns out that what's happening is that the type of &Derived::v2
is not int Derived::*
, but int Base2::*
, which naturally does have an offset of zero because it's the offset with respect to Base2. When you explicitly convert it to an int Derived::*
, the offset is corrected.
VC ++或GCC或Clang ...我正在使用stdio / printf作为海报使用。
Try this code on VC++ or GCC or Clang... I'm sticking with stdio/printf as the poster was using.
struct Base1 { int a; };
struct Base2 { int b; };
struct Derived : Base1, Base2 { };
#include <cassert>
#include <cstdio>
#include <typeinfo>
using namespace std;
int main () {
printf( "%s\n", typeid(&Derived::a).name() ); // mentions Base1
printf( "%s\n", typeid(&Derived::b).name() ); // mentions Base2
int Derived::* pdi = &Derived::b; // OK
int Base2::* p2i = &Derived::b; // OK
//int Base1::* p1i = &Derived::b; // ERROR
assert( sizeof(int*) == sizeof(pdi) );
printf( "%p %p", p2i, pdi ); // prints "(nil) 0x4" using GCC 4.8 at liveworkspace.org
}
这篇关于在Visual Studio中C ++指向类数据成员的指针的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!