【问题标题】:Pure virtual operator纯虚拟算子
【发布时间】:2013-05-22 10:42:05
【问题描述】:

我有一个 C++ 学校项目,但我被困在一个部分: 我必须重载运算符 + 和 * 才能处理几何图形。那没问题,但在这里它不起作用:我必须将运算符声明为纯虚方法,在所有其他类派生的抽象类中。

#include<iostream>
using namespace std;

class Figabs {
protected:
    int fel;
public:
    int getFEL() { return fel; }
    virtual Figabs operator +()=0; /*this is where I get an error: function returning  abstract class “Figabs” is not allowed : function Figabs::operator+ is a pure virtual function */
};

class Coord {
public: 
    int cx, cy; 
public: 
    Coord (){ 
        cx = cy = 0;
    }

    Coord (const int x, const int y) {
        cx = x;
        cy = y;
    }

    Coord (const Coord &din) { 
        cx = din.cx;
        cy = din.cy;
    }

    ~Coord () { }
    void setX(const int val) { cx = val; } ;
    void setY(const int val) { cy = val; };
    int getX() { return cx; }
    int getY() { return cy; }
};

class Point : public Coord, public Figabs { //one of the figures

public:
    Point() { 
        setX(0);
        setY(0);
        fel = 0;
    }

    Point(const int x, const int y): Coord (x,y) { 
        fel = 0;
    } 

    Point(const Point &din): Coord (din) { 
        fel = din.fel; 
    } 

    ~Point() { } 

    Point operator +(const Coord &vector) { /*this works perfectly when I delete the declaration from the abstract class Figabs, but I don’t know how to make them work together */
        int xp = cx + vector.cx;
        int yp = cy + vector.cy;
        return (Point (xp, yp));
    }

    Point operator *(const Coord &vector) {
        Point temp;
        temp.cx = cx * vector.cx;
        temp.cy = cy * vector.cy;
        return (temp);
    } 
};

谢谢你,请耐心等待,这是我第一次接触 C++。

【问题讨论】:

  • @Shark:那到底会发生什么变化?
  • virtual Figabs operator +()=0 with no arguments -> Point operator +(const Coord &vector) 返回不同的类型?他们的签名必须相同...
  • 在没有 RHS 的操作员摘要的情况下,我仍在努力理解您的期望? (或者我错过了什么)?即使您确实提供了适当的操作,您的 retval 也会切片,顺便说一句。对于第一次 C++ 问题,您遇到了 很多 微妙的问题,所以对此表示欢迎 =P
  • 这是一个有点棘手的问题,因为operator+应该返回一个对象(不是引用),所以它不能返回一个抽象类(Figabs在这里是因为它有一个纯虚函数)。这在分配的约束下很难解决,至少部分原因是分配执行了糟糕的设计operator+ 通常不应是成员函数(GOTW #4, part 5)
  • @BoBTFish 解决起来很棘手,因为二元运算符的重载必须按值返回,而按值返回和多态不能很好地协同工作。完全没有。

标签: c++ operator-overloading pure-virtual


【解决方案1】:

正如其他发帖人所指出的,这项任务远非 微不足道,operator+ 通常不是成员。那里有两个 应该解决的问题:

  1. 如果你支持`FigAbs + Coord`,那么你也应该支持 `坐标 + FigAbs`。第一个可以是会员(没有真实的 那里的问题);第二,如果要成为会员,必须是 `Coord` 的成员,这可能不是我们想要的。
  2. `operator+` 的任何合理实现都必须通过 价值。而且您不能(通常)通过以下方式返回多态类 价值;你需要像信封成语这样的东西 这个工作:基类必须看起来像: 类图:BinaryOperators { 图* myImpl; 上市: 图&运算符+=(坐标常数&翻译) { myImpl->operator+=(翻译); 返回*这个; } }; 当然,您需要正确的工厂方法 为每种不同的类型实例化“Figure”,一个虚拟的 `clone` 函数,以及复制构造函数、赋值和 支持深拷贝的析构函数。 (`BinaryOperators` 是 一个模板类,它在以下方面实现了 `operator+` `运算符+=`;这是提供二进制文件的常用方法 运营商。)

最后,我认为这是运算符重载滥用。 加法的概念不适用于几何图形。 你在做的是所谓的翻译,而逻辑 解决方案是提供一个成员函数来完成它,而不是 重载加法。

【讨论】:

    【解决方案2】:

    Figabs 包含一个纯虚成员函数virtual Figabs operator +()=0; 这意味着你不能实例化Figabs

    考虑:

    virtual Figabs& operator +()=0; 
    /*Now you will not be returning an actual instance but can return derived class instances*
    

    【讨论】:

    • 返回值指向哪里?
    • 谢谢,这让错误消失了,但现在派生类,我有: Puncte operator +(const Coord &vector) 说错误:返回类型与返回类型不同也不协变Figabs& 被覆盖的虚函数 Figab::operator+
    • @BoBTFish 我完全理解并同意。这是一个非常具体的答案,提请注意编译器错误的原因不是问题的完整解决方案。
    【解决方案3】:

    请查看以下链接以获取与问题相关的有用信息

    overriding virtual function return type differs and is not covariant

    virtual Figabs operator +() = 0;//Here ur not passing any i/p parameters
    

    但是在派生类中传递参数

    Point operator +(const Coord &vector)//here ur sending i/p parameter .
    

    【讨论】:

    • 这更像是一个评论而不是一个答案。
    猜你喜欢
    • 2019-10-21
    • 2012-12-02
    • 2010-11-21
    • 1970-01-01
    • 2019-02-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多