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 位编译模式下,virtual在 Aix 系统上运行,没有对齐编译指示、没有优化、没有函数等。

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

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

这些是链接器选项:

-brtl -bernotok -G

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

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

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

任何想法?

4

2 回答 2

1

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

于 2011-09-08T11:35:08.323 回答
0

在你的 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.
        }
    }
于 2011-09-08T11:28:10.103 回答