x64类型转换不正确的值

时间:2016-10-25 05:04:20

标签: c++ 64-bit

我们正在使用VS2013。请在下面找到示例代码,我们得到的dcp_p变量值不正确。 相同的示例在x86平台上运行良好。

#include "stdafx.h"

template <class T>
class MyPointer
{
  public:
     MyPointer(void) : func1_p(0), func2_p(0), func3_p(0),  
          func4_p(0), func5_p(0), func6_p(0), msgFlags_(false) {}

  private:
      bool (T::*func1_p)(void);

       void (T::*func2_p)(void);

       bool (T::*func3_p)(void);

       void (T::*func4_p)(const char *);

       void (T::*func5_p)(const char *);

       void (T::*func6_p)(const char *);

        bool  msgFlags_;
  };


  class dummy{};
  class BaseOne{};
  class BaseTwo{};

  class Derived : public BaseOne, public BaseTwo
  {

   public:
         MyPointer<Derived> dcp;
         void create()
         {

        MyPointer<dummy> *dcp_p = (MyPointer<dummy> *)(&(this->dcp));

       // When we typecast, the dcp_p holds the incorrect values instead of the values assigned by ctor (ie, 0)
      // If multiple inheritance is removed, and have only one base class, dcp_p holds correct value.
     // In x86 platform, the dcp_p values are correct even if multiple inheritance is there.

     printf("%s", "Success");
        }

  };

   int _tmain(int argc, _TCHAR* argv[])
   {
       Derived *d = new Derived();
       d->create();
       return 0;
    }

编译器输出:/ d1reportAllClassLayout flag

  class dummy size(1):
  1>   +---
  1>   +---
  1>  
  1>  
  1>  
  1>  class BaseOne size(1):
  1>   +---
  1>   +---
  1>  
  1>  
  1>  
  1>  class BaseTwo size(1):
  1>   +---
  1>   +---
  1>  
  1>  
  1>  
  1>  class ?$MyPointer@VDerived@@ size(104):
  1>   +---
  1>   0 | func1_p
  1>  16 | func2_p
  1>  32 | func3_p
  1>  48 | func4_p
  1>  64 | func5_p
  1>  80 | func6_p
  1>  96 | msgFlags_
  1>     | <alignment member> (size=7)
  1>   +---

  class Derived size(112):
  1>   +---
  1>   | +--- (base class BaseOne)
  1>   | +---
  1>   | +--- (base class BaseTwo)
  1>   | +---
  1>     | <alignment member> (size=7)
  1>   8 | ?$MyPointer@VDerived@@ dcp
  1>   +---

dcp_p的示例输出:

this = 0x0000000000433D20

dcp_p = 0x0000000000433D28

0x0000000000433D28 00 00 00 00 00 00 00 00 00 00 00 00 cc cc cc cc 00 00 00 00 00 00 00 00 00 00 00 00 cc cc cc cc 00 00 00 00 00 00 00 00 00 00 00 00 cc cc cc cc 00 00 00 00 00 00 00
0x0000000000433D5F 00 00 00 00 00 cc cc cc cc 00 00 00 00 00 00 00 00 00 00 00 cc cc cc cc 00 00 00 00 00 00 00 00 00 00 00 cc cc cc cc

为什么在使用多重继承和x64位时值不正确。?

所有(func_p)的sizeof都是16.

另外,对于MyPointer dcp; //所有字节值都为零。

所以,问题是,当我们对类型为何错误进行类型转换时。?

提前致谢

0 个答案:

没有答案