如果从同一基类相互作用的两个不同的派生类对象,如何提出异常?

发布于 2025-01-24 00:38:24 字数 811 浏览 1 评论 0原文

我有一个带有A&的抽象基类(x) B派生的类。

我在X中有一个类方法,该方法由两个类A& B.

我希望该方法能够引发异常,如果两个不同的派生类对象相互交互。

这是一个人为的例子:

class Food{
    public:
        int count;
        void combine(Food* a) {
            this->count += a->count;
        }
};


class Orange : Food {
    public:
        Orange(int x) {
            count = x;
        }
};

class Apple : Food{
    public:
        Apple(int x) {
            count = x;
        }
};


int main() {
    Food* basket_apples = new Apple(5);
    Food* basket_oranges = new Orange(4);
    Food* crate_oranges = new Orange(10);
    crate_oranges.combine(basket_oranges);//should work fine
    crate_oranges.combine(basket_apples); //should produce error
}

我考虑的解决方案是覆盖两个派生类中的联合方法,但这违反了干燥(不要重复自己)。

我想知道是否还有其他选择可以解决此问题。

I have an abstract base class(X) with A & B derived classes.

I have a class method in X that is inherited by both classes A & B.

I want the method to be able to throw an exception if two different derived class objects interact with each other.

Here is a contrived example:

class Food{
    public:
        int count;
        void combine(Food* a) {
            this->count += a->count;
        }
};


class Orange : Food {
    public:
        Orange(int x) {
            count = x;
        }
};

class Apple : Food{
    public:
        Apple(int x) {
            count = x;
        }
};


int main() {
    Food* basket_apples = new Apple(5);
    Food* basket_oranges = new Orange(4);
    Food* crate_oranges = new Orange(10);
    crate_oranges.combine(basket_oranges);//should work fine
    crate_oranges.combine(basket_apples); //should produce error
}

A solution I considered is to override the combine method in both derived classes but that violates DRY(Don't repeat yourself).

I want to know if there are any other options to solve this issue.

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

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

发布评论

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

评论(1

ζ澈沫 2025-01-31 00:38:24

您可以在组合函数中检查一下:

void combine(Food const& a) // You should pass your argument by const ref
{
    assert(typeid(*this) == typeid(a)); // This checks ONLY IN DEBUG !
    this->count += a.count;
}

如果要管理错误,使用异常或返回值:

void combine(Food const& a)
{
    if(typeid(*this) != typeid(a))
      throw std::runtime_error("Invalid combined types");
    this->count += a.count;
}


int combine(Food const& a)
{
    if(typeid(*this) != typeid(a))
      return 1;
    this->count += a.count;
    return 0;
}

也许您应该转移计数器?

void combine(Food& a) // Passing by non-const reference
{
    assert(typeid(*this) == typeid(a));
    this->count += a.count;
    a.count = 0;
}

注意:如用户17732522所说,您的成员函数组合应该是虚拟的,并且类应该更好地管理继承。我建议这样做:

class Food
{
    protected:
        std::size_t count; // You want this value >= 0
    public:
        Food(std::size_t count_) : count(count_) {}
        Food(Food const& f) : count(f.count) {}
        virtual ~Food() {}
        virtual std::string name() const = 0;
        virtual void combine(Food const& a)
        {
            assert(typeid(a) == typeid(*this));
            this->count += a.count;
        }
};


class Orange : public Food
{
    public:
        Orange(std::size_t x) : Food(x) {}
        Orange(Orange const& o) : Food(o) {}
        virtual ~Orange() {}
        virtual std::string name() const { return "orange"; }
};

class Apple : public Food
{
    public:
        Apple(std::size_t x) : Food(x) {}
        Apple(Apple const& a) : Food(a) {}
        virtual ~Apple() {}
        virtual std::string name() const { return "apple"; }
};

You can check that in the combine function :

void combine(Food const& a) // You should pass your argument by const ref
{
    assert(typeid(*this) == typeid(a)); // This checks ONLY IN DEBUG !
    this->count += a.count;
}

If you want to manage the error, use exception or return value :

void combine(Food const& a)
{
    if(typeid(*this) != typeid(a))
      throw std::runtime_error("Invalid combined types");
    this->count += a.count;
}


int combine(Food const& a)
{
    if(typeid(*this) != typeid(a))
      return 1;
    this->count += a.count;
    return 0;
}

Maybe you should transfert the counter ?

void combine(Food& a) // Passing by non-const reference
{
    assert(typeid(*this) == typeid(a));
    this->count += a.count;
    a.count = 0;
}

Note: As said by user17732522, your member function combine should be virtual and the class should better manage the inheritence. I suggest this :

class Food
{
    protected:
        std::size_t count; // You want this value >= 0
    public:
        Food(std::size_t count_) : count(count_) {}
        Food(Food const& f) : count(f.count) {}
        virtual ~Food() {}
        virtual std::string name() const = 0;
        virtual void combine(Food const& a)
        {
            assert(typeid(a) == typeid(*this));
            this->count += a.count;
        }
};


class Orange : public Food
{
    public:
        Orange(std::size_t x) : Food(x) {}
        Orange(Orange const& o) : Food(o) {}
        virtual ~Orange() {}
        virtual std::string name() const { return "orange"; }
};

class Apple : public Food
{
    public:
        Apple(std::size_t x) : Food(x) {}
        Apple(Apple const& a) : Food(a) {}
        virtual ~Apple() {}
        virtual std::string name() const { return "apple"; }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文