c++ - -Woverloaded-const 函数的虚拟警告

标签 c++ class operator-overloading declaration gcc-warning

我有这段代码:

class ISerializable
{
        public:
            virtual bool operator==(const ISerializable* /*value*/) const { return false;};
            virtual bool operator!=(const ISerializable* /*value*/) const { return true;};
};

class Point2I : public ISerializable
{
        public:
            bool operator==(const Point2I& value)
            {
                return (x == value.x && y == value.y);
            }

            bool operator!=(const Point2I& value)
            {
                return !(*this == value);
            }
            public:
            int x;
            int y;
};

class Coordinate : public ISerializable
{
         public:
            virtual bool operator==(const Coordinate& value) const;
            virtual bool operator!=(const Coordinate& value) const;
};

这导致我在 gcc 编译器上发出 -Woverloaded-virtual 警告。

我理解此警告,因为 Point2I 中的函数声明隐藏了 ISerialized 中的虚拟函数。 但我不确定在 Point2I 中缺少 const 是否会导致此警告。

您能否帮我了解是否是 const 导致了此警告或其他原因? gcc 的警告描述没有具体提及任何内容。

更新:

我在我的代码库中发现了另一个类坐标,它已经覆盖了这个,并且 gcc 没有为此抛出警告。 Point2I坐标 的唯一区别是我没有在 Point2I 中将其声明为 virtual with const 。看起来只是 const 隐藏了基类声明。

最佳答案

if it is const which is causing this warning or something else?

我会说这是别的东西,即您实际上并没有重写基类方法,即使您添加 const .

论点const ISerializable*const Point2I& 不同.


一种解决方案可能是使用 const ISerializable& 覆盖基类方法。作为参数,并在重写的方法中进行强制转换:

class ISerializable {
public:
    // add virtual destructor if you need to delete objects through
    // base class pointers later:
    virtual ~ISerializable() = default;
    virtual bool operator==(const ISerializable&) const { return false; }
    virtual bool operator!=(const ISerializable&) const { return true; }
};

class Point2I : public ISerializable {
public:
    bool operator==(const ISerializable& value) const override {        
        auto rhs = dynamic_cast<const Point2I*>(&value);
        // rhs will be nullptr if the cast above fails
        return rhs && (x == rhs->x && y == rhs->y);
    }

    bool operator!=(const ISerializable& value) const override {
        return !(*this == value);
    }

private:
    int x = 0;
    int y = 0;
};

使用示例:

#include <iostream>

class Foo : public ISerializable {   // another ISerializable 
public:
};

int main() {
    Point2I a, b;
    std::cout << (a == b) << '\n';  // true  - using Point2I::operator==

    Foo f;
    std::cout << (a == f) << '\n';  // false - using Point2I::operator==
    std::cout << (f == a) << '\n';  // false - using ISerializable::operator==

    // this makes the default implementation in ISerializable utterly confusing:
    std::cout << (f == f) << '\n';  // false - using ISerializable::operator==
}

另一种可能的解决方案是使用 CRTP但如果您想比较从 ISerializable<T> 派生的不同类型,这将不起作用:

template<class T>
class ISerializable {
public:
    virtual ~ISerializable() = default;
    virtual bool operator==(const T&) const = 0;
    virtual bool operator!=(const T&) const = 0;
};

class Point2I : public ISerializable<Point2I> {
public:
    bool operator==(const Point2I& value) const override {
        return (x == value.x && y == value.y);
    }

    bool operator!=(const Point2I& value) const override {
        return !(*this == value);
    }

public:
    int x;
    int y;
};

关于c++ - -Woverloaded-const 函数的虚拟警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68770374/

相关文章:

c++ - OpenGL纹理

java - 如何从原始类文件中找出依赖的类?

python - 我对 Blender 中的 Python 脚本有一个严重的问题

c++ - 基类中运算符重载的问题

c++ - 隐式定义的用户转换运算符支持的操作

c++ - CMake 冲突的头文件

c++ - Lambda 作为函数参数

Jquery:使用addClass突出显示 'like'菜单项

c++ - 为什么STL中List的迭代器使用的是(*node).data而不是node->data?

java - 用于流量控制的 Log4X