具有继承和虚拟基类的赋值运算符

发布于 2024-08-17 06:46:02 字数 955 浏览 6 评论 0原文

我有一个抽象虚拟基类 Foo,从中我派生出许多其他有细微差别的类。我有一个创建派生类并返回 Foo* 的工厂。我最大的问题之一是运算符重载,我需要确保 DFoo 不会被 DFoo1 操作(未显示)。我目前已经通过检查强制转换是否失败来处理此问题,但我对这种方法非常不满意。我必须使用基实现,因为我只能从工厂返回基类。如果这是最好的方法,那很好,我只是想确保这是有道理的,并且没有我遗漏的模式。非常感谢任何有关如何处理此类事情的建议。

 class Foo
 {
    public:
          Foo(int x){...};
          Bar m_bar;
          virtual Foo& operator=(const Foo& f)
          {
             m_bar = f.m_bar
          }
 }

现在,我的派生类

class DFoo : public Foo
{
     DFoo(int x, int y):Foo(int x) {...}
     FooBar m_foobar;

     Foo& operator=(const Foo& rhs)
     {
        if(this != &rhs)
        {

              Foo::operator=(rhs);
              DFoo temp = static_cast<DFoo>(rhs);

              if(temp != NULL)
              {
                m_foobar = static_cast<DFoo>(rhs).m_foobar;
              }
              else
                 throw exception(ex);
        }
     }
}

I have an abstract virtual base class Foo from which I derive many other classes that differ in small ways. I have a factory that creates the derived classes and returns Foo*. One of my bigger problems is in my operator overloads, I need to make sure that the DFoo does not get operated on by DFoo1 (not shown). I have currently handled this with checking if a cast fails, but I'm pretty unhappy with that approach. I have to use the base implementation, because I only can return the base class from the factory. If this is the best way to do it, that's fine, I just want to make sure that this makes sense and that there isn't a pattern I'm missing. Any suggestions about how to handle this sort of thing are very much appreciated.

 class Foo
 {
    public:
          Foo(int x){...};
          Bar m_bar;
          virtual Foo& operator=(const Foo& f)
          {
             m_bar = f.m_bar
          }
 }

Now, my derived class

class DFoo : public Foo
{
     DFoo(int x, int y):Foo(int x) {...}
     FooBar m_foobar;

     Foo& operator=(const Foo& rhs)
     {
        if(this != &rhs)
        {

              Foo::operator=(rhs);
              DFoo temp = static_cast<DFoo>(rhs);

              if(temp != NULL)
              {
                m_foobar = static_cast<DFoo>(rhs).m_foobar;
              }
              else
                 throw exception(ex);
        }
     }
}

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

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

发布评论

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

评论(3

蓝海 2024-08-24 06:46:02

您可能正在寻找 boost::noncopyable

You are probably looking for boost::noncopyable.

浮生未歇 2024-08-24 06:46:02

在这种情况下,最明智的做法是声明赋值运算符和复制构造函数,但不定义它们。然后,如果代码中的某个地方有人尝试制作副本,则会生成链接器错误。这本质上是 boost::nocopyable 的效果,只不过您没有为这样一个简单而琐碎的任务引入外部库。

编辑:此外,如果您将构造函数和运算符设置为私有,您将收到编译器错误。

The smartest thing to do in this situation is to declare assignment operators and copy constructors but not define them. Then if somewhere in your code someone tries to make a copy it will generate a linker error. This is essentially the effect that boost::nocopyable has except that you're not bringing in an external library for such a simple and trivial task.

Edit: Also if you make the constructor and operator private you will get a compiler error.

十年九夏 2024-08-24 06:46:02

您永远不应该这样做:

 class DFoo
 {
     Foo& operator=(const Foo& rhs) ;
 };

除非您明确希望支持将基类分配给派生类型(不太可能)。
allocateemtn 运算符应如下所示:

 class DFoo
 {
     DFoo& operator=(DFoo const& rhs);  // You can only assign DFoo to a DFoo.
                                        // Now there is no way for DFoo1 to get assigned to a DFoo
 };

另外,在这个简单的示例中,您发布了由编译器生成的默认赋值运算符应该按预期工作。您编写赋值运算符有什么特殊原因吗?

You should never do this:

 class DFoo
 {
     Foo& operator=(const Foo& rhs) ;
 };

Unless you explicitly want to support the assignment of a base class to a derived type (unlikely).
The assignemtn operator should look like this:

 class DFoo
 {
     DFoo& operator=(DFoo const& rhs);  // You can only assign DFoo to a DFoo.
                                        // Now there is no way for DFoo1 to get assigned to a DFoo
 };

Also in this trivial example that you posted the default assignent operator generated by the compiler should work as expected. Is there some special reason that you are writting an assignment operator?

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