不同的对象指针值及其this指针

发布于 2024-12-03 16:38:05 字数 1181 浏览 8 评论 0原文

基本上,我编写了一个类,其中包含另一个类数组属性,我的意思是:

class MyClass
{
  unsigned long long x_;
  bool y_;

  public:
  MyClass & operator=(const MyClass & mc)
  {
    x_ = mc.x_;
    y_ = mc.y_;
    return *this;
  }
};

class MyOtherClass
{
  MyClass myClass_[9];

  public:
  MyOtherClass & operator=(const MyOtherClass & mc)
  {
    for (unsigned int i = 0; i < 9; i++)
    {
      myClass_[i] = mc.myClass_[i];
    }
    return *this;
  }
};

所有这些都在共享库中实现。

我在第二个库中使用第二个类,例如:

void ThridClass::foo( )
{
  MyOtherClass c1;
  MyOtherClass c2;
  c1 = c2;
}

在 xlC_7 的 64 位编译模式下,没有对齐编译指示、没有优化、非虚拟函数等,在 Aix 系统上运行。

这些是我在两个库中使用的编译器选项:

-q64 -bernotok -g -M -qflag=i:w

这些是链接器选项:

-brtl -bernotok -G

当我使用 dbx 调试程序并询问 c1.myClass_[0] 地址时,我得到了一个值。但是,如果我跟踪 MyOtherClass.operator=() 函数内部的执行,我会得到该属性指针的另一个地址。

(dbx) p &c1.myClass_[0]
0x0ffffffffffe6a20
(dbx) s
stopped in operator=(const MyOtherClass &)
(dbx) p &myClass_[0]
0x0ffffffffffe69c0

这个问题在 Linux 上不会出现并且工作正常。

有什么想法吗?

basically, I wrote a class with another class array atributte inside, I mean:

class MyClass
{
  unsigned long long x_;
  bool y_;

  public:
  MyClass & operator=(const MyClass & mc)
  {
    x_ = mc.x_;
    y_ = mc.y_;
    return *this;
  }
};

class MyOtherClass
{
  MyClass myClass_[9];

  public:
  MyOtherClass & operator=(const MyOtherClass & mc)
  {
    for (unsigned int i = 0; i < 9; i++)
    {
      myClass_[i] = mc.myClass_[i];
    }
    return *this;
  }
};

all of this implemented in a shared library.

I use second class in a second library like:

void ThridClass::foo( )
{
  MyOtherClass c1;
  MyOtherClass c2;
  c1 = c2;
}

in a 64bit compilation mode with xlC_7, with no alignment pragmas, no optimization, non virtual functions, etc, running on an Aix system.

These are the compiler options I used in both libraries:

-q64 -bernotok -g -M -qflag=i:w

and these are linker options:

-brtl -bernotok -G

When I debug the program using dbx and ask for c1.myClass_[0] address I got one value. But, if I strace the execution inside MyOtherClass.operator=() function, I get another address for this attribute pointer.

(dbx) p &c1.myClass_[0]
0x0ffffffffffe6a20
(dbx) s
stopped in operator=(const MyOtherClass &)
(dbx) p &myClass_[0]
0x0ffffffffffe69c0

This problem doesn't apperar on Linux and works fine.

Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

此刻的回忆 2024-12-10 16:38:05

只需放弃复制赋值和复制构造函数定义 - 隐式编译器生成的定义就足够了。

Just ditch your copy assignment and copy constructor definitions- the implicit compiler-generated ones will be more than sufficient.

罗罗贝儿 2024-12-10 16:38:05

在你的operator=中你返回循环内部

MyOtherClass & MyOtherClass::operator=(const MyOtherClass & mc)
    {
        for (unsigned int i = 0; i < 9; i++)
        {
            myClass_[i] = mc.myClass_[i];
            return *this; ////                Move this outside the loop.
        }
    }

In your operator= you return inside the loop

MyOtherClass & MyOtherClass::operator=(const MyOtherClass & mc)
    {
        for (unsigned int i = 0; i < 9; i++)
        {
            myClass_[i] = mc.myClass_[i];
            return *this; ////                Move this outside the loop.
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文