当类是子类时重载赋值运算符

发布于 2024-12-20 06:26:52 字数 503 浏览 3 评论 0原文

如何使用赋值运算符实现设置基类成员?例如,如果有人在派生类中定义赋值运算符,如下所示:(

其中 colourColour() 都是基类的成员 - 意味着下面指示的行是非法)

Derived& Derived::operator=(const Derived& rhs) 
{
if (&rhs != this)
{

    Colour(rhs.colour);    // not allowed
        Colour(rhs.Colour());  // not allowed
}
return *this;
}

解决办法是什么?有没有办法在基础中链接运算符重载?我是否要做类似的事情...

Derived& Derived::operator=(const Derived& rhs) : Base::operator=(rhs)
...?

How do you set base class members using the assignment operator implementation? If for example someone defines the assignment operator in a derived class like this:

(where both colour and Colour() are members of the base class - meaning the lines indicated below are illegal)

Derived& Derived::operator=(const Derived& rhs) 
{
if (&rhs != this)
{

    Colour(rhs.colour);    // not allowed
        Colour(rhs.Colour());  // not allowed
}
return *this;
}

what is the solution? Is there a way of linking operator overloads in the base? Do I do something like...

Derived& Derived::operator=(const Derived& rhs) : Base::operator=(rhs)
...?

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

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

发布评论

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

评论(4

等待我真够勒 2024-12-27 06:26:52

它是这样完成的:

class B
{
 public:
  B& operator=( const B & other )
  {
    v = other.v;
    return *this;
  }
  int v;
};

class D : public B
{
 public:
  D& operator=( const D & other )
  {
    B::operator=( other );
    return *this;
  }
};

It is done like this :

class B
{
 public:
  B& operator=( const B & other )
  {
    v = other.v;
    return *this;
  }
  int v;
};

class D : public B
{
 public:
  D& operator=( const D & other )
  {
    B::operator=( other );
    return *this;
  }
};
白首有我共你 2024-12-27 06:26:52

你已经很接近了,只需将该调用放入方法主体中即可。

 if (&rhs != this)
 {
    Base::operator=(rhs);
    // ...

You're close, just put that call in the method body.

 if (&rhs != this)
 {
    Base::operator=(rhs);
    // ...
瑕疵 2024-12-27 06:26:52

您应该能够使用公共访问器和修改器:

Derived& Derived::operator=(const Derived& rhs) 
{
   if (&rhs != this)
      SetColour(rhs.GetColour());
   return *this;
}

否则,请在基类中保护成员,以便派生类可以访问:

Derived& Derived::operator=(const Derived& rhs) 
{
   if (&rhs != this)
      colour = rhs.colour;
   return *this;
}

第三种选择可能是在基类中定义公共赋值运算符,并让派生类调用基类操作员:

Derived& Derived::operator=(const Derived& rhs) 
{
   if (&rhs != this)
      Base::operator=(rhs);
   return *this;
}

这是一个完整的测试用例:

#define TEST 2
class Base
{
public:
    Base() : m_protected(0), m_private(0) {}
    Base(int pro, int pri) : m_protected(pro), m_private(pri) {}
    ~Base() {}

#if TEST == 1
    Base& operator=(const Base& rhs)
    {
        if (this != &rhs)
        {
            m_protected = rhs.m_protected;
            m_private = rhs.m_private;
        }

        return *this;
    }
#elif TEST == 2
    void SetPrivate(int i) { m_private = i; }
    int GetPrivate() const { return m_private; }
#endif

protected:
    int m_protected;
private:
    int m_private;
};

class Derived : public Base
{
public:
    Derived() : Base() {}
    Derived(int pro, int pri) : Base(pro, pri) {}
#if TEST == 1
    Derived& operator=(const Derived& rhs)
    {
        Base::operator=(rhs);
        return *this;
    }
#elif TEST == 2
    Derived& operator=(const Derived& rhs)
    {
        if (this != &rhs)
        {
            SetPrivate(rhs.GetPrivate());
            m_protected = rhs.m_protected;
        }
        return *this;
    }
#endif
};

int main()
{
    Derived a;
    Derived b(10, 5);

    a = b;
        return 0;
}

You should be able to use public accessors and mutators:

Derived& Derived::operator=(const Derived& rhs) 
{
   if (&rhs != this)
      SetColour(rhs.GetColour());
   return *this;
}

Otherwise make the members protected in the base class so that derived classes have access:

Derived& Derived::operator=(const Derived& rhs) 
{
   if (&rhs != this)
      colour = rhs.colour;
   return *this;
}

A third option could be to define an assignment operator public in the base class and have your derived class call the base operator:

Derived& Derived::operator=(const Derived& rhs) 
{
   if (&rhs != this)
      Base::operator=(rhs);
   return *this;
}

Here's a complete test case:

#define TEST 2
class Base
{
public:
    Base() : m_protected(0), m_private(0) {}
    Base(int pro, int pri) : m_protected(pro), m_private(pri) {}
    ~Base() {}

#if TEST == 1
    Base& operator=(const Base& rhs)
    {
        if (this != &rhs)
        {
            m_protected = rhs.m_protected;
            m_private = rhs.m_private;
        }

        return *this;
    }
#elif TEST == 2
    void SetPrivate(int i) { m_private = i; }
    int GetPrivate() const { return m_private; }
#endif

protected:
    int m_protected;
private:
    int m_private;
};

class Derived : public Base
{
public:
    Derived() : Base() {}
    Derived(int pro, int pri) : Base(pro, pri) {}
#if TEST == 1
    Derived& operator=(const Derived& rhs)
    {
        Base::operator=(rhs);
        return *this;
    }
#elif TEST == 2
    Derived& operator=(const Derived& rhs)
    {
        if (this != &rhs)
        {
            SetPrivate(rhs.GetPrivate());
            m_protected = rhs.m_protected;
        }
        return *this;
    }
#endif
};

int main()
{
    Derived a;
    Derived b(10, 5);

    a = b;
        return 0;
}
呆头 2024-12-27 06:26:52

如果您想从派生类调用基类运算符=,我会在基类运算符中实现分配/构造颜色的operator=功能:

Base::operator=(rhs)

在派生类operator=()实现中。据我所知,您为 Derived operator= 提议的签名不是有效的 C++。

I implement the operator= functionality assigning/constructing color in the Base class operator if you want to call the Base operator= from the Derived class use:

Base::operator=(rhs)

in the Derived class operator=() implementation. The signature you've proposed for Derived operator= isn't valid C++ as far as I know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文