有没有一种聪明的方法可以禁止通过派生类之一隐藏基类的运算符函数

Is there a smart way to inhibit hiding a operator function of base class by one of derived class

本文关键字:派生 隐藏 函数 运算符 基类 禁止 一种 有没有 方法      更新时间:2023-10-16

我正在学习c++一段时间。 我想创建一个具有奇怪递归模板模式的指针类,如下所示。不幸的是,它不起作用,因为派生类的默认运算符==()函数隐藏了基类的运算符==()函数。我们是否有一种聪明的方法来抑制派生类之一隐藏基类的运算符函数?或者你知道避免这个问题的方法吗?以防万一,我附加来自编译器的消息。谢谢。

#include <vector>
template <typename Derived, typename T>
class Function0 {
public:
    friend bool operator== (Derived& org, Derived& cmp) {  // ***** This line gives a warning
        Derived& org_value = static_cast<Derived&>(org);
        Derived& cmp_value = static_cast<Derived&>(cmp);
        return *(org_value.value_) == *(cmp_value.value_);  // *** old code: return org_value == cmp_value;
    }
};
template <typename T, template <typename Derived, typename T_T> class Functions>
class Pointer : public Functions<Pointer<T,Functions>, T> {
public:
    Pointer() {};
    Pointer(T* new_value) : value_(new_value) {};
    bool operator== (Pointer& cmp) = delete;  // ***** This line gives a warning
    virtual ~Pointer() {
};
private:
    friend Functions<Pointer<T,Functions>, T>;
    T* value_ = nullptr;
};
class TestA {
public:
    TestA(unsigned int id) : id_(id) {};
    virtual ~TestA() {};
    unsigned int id(void) { return id_; }
    bool operator== (TestA& cmp) {
        return (id_ == cmp.id()) ? true : false;
    }
private:
    unsigned int id_ = 0;
};
template <typename Element>
Element findCorrespondingFirst(Element& obj, std::vector<Element>& vec) {
    for (unsigned int i = 0; i < vec.size(); ++i) {
        auto o = vec[i];
        if (obj == o) {  // this dispatches an error massage
            return o;
        }
    }
    return Element();
}
void test_pointer_class(void) {
    std::vector<Pointer<TestA, Function0>> ptr_vector;
    TestA* raw_ptr0 = new TestA(1);
    TestA* raw_ptr1 = nullptr;
    TestA* raw_ptr2 = new TestA(2);
    Pointer<TestA, Function0> ptr0 = Pointer<TestA, Function0>(raw_ptr0);
    Pointer<TestA, Function0> ptr1 = Pointer<TestA, Function0>(raw_ptr1);
    Pointer<TestA, Function0> ptr2 = Pointer<TestA, Function0>(raw_ptr2);
    TestA* raw_ptr3 = new TestA(1);
    Pointer<TestA, Function0> ptr3 = Pointer<TestA, Function0>(raw_ptr3);
    ptr_vector.push_back(ptr0);
    ptr_vector.push_back(ptr1);
    ptr_vector.push_back(ptr2);
    ptr_vector.push_back(ptr3);
    auto result1 = findCorrespondingFirst(ptr3, ptr_vector);
    delete raw_ptr0;
    delete raw_ptr1;
    delete raw_ptr2;
    delete raw_ptr3;
}

来自 gcc 编译器的部分消息如下:

/home/.../pointer.hpp: In instantiation of ‘Element findCorrespondingFirst(Element&, std::vector<_RealType>&) [with Element = Pointer<TestA, Function0>]’:
/home/.../pointer.hpp:120:56:   required from here
/home/.../pointer.hpp:68:11: error: ambiguous overload for ‘operator==’ (operand types are ‘Pointer<TestA, Function0>’ and ‘Pointer<TestA, Function0>’)
   if (obj == o) {  // this dispatches an error massage
           ^
/home/.../pointer.hpp:68:11: note: candidates are:
/home/.../pointer.hpp:39:7: note: bool Pointer<T, Functions>::operator==(Pointer<T, Functions>&) [with T = TestA; Functions = Function0] <deleted>
    bool operator== (Pointer& cmp) = delete;  // ***** This line gives a warning
   ^
/home/.../pointer.hpp:23:14: note: bool operator==(Pointer<TestA, Function0>&, Pointer<TestA, Function0>&)
friend bool operator== (Derived& org, Derived& cmp) {  // ***** This line gives a warning
          ^

再次非常感谢。

bool operator== (Pointer& cmp) = delete;  // ***** This line gives a warning

在很多方面都是错误的。事实上,删除它可以解决问题(你应该考虑这样做,因为目前还不清楚它的目的是什么,我必须说)。

顺便说一下,定义它的正确方法是将其声明为

bool operator== (const Pointer<T, Functions>& cmp) const = delete;  // ***** This line gives a warning

请注意,您的类类型不是Pointer,而是 Pointer<T, Functions> ,以及此运算符的正确const属性。

您的第一个operator==也存在常量问题。


我建议您进行以下更改:

friend bool operator== (const Derived& org, const Derived& cmp) {  // ***** This line gives a warning
    const Derived& org_value = static_cast<const Derived&>(org);
    const Derived& cmp_value = static_cast<const Derived&>(cmp);                                                                                                                                         
    return org_value == cmp_value;
}   

只需删除后者:

// bool operator== (const Pointer<T, Functions>& cmp) const = delete;  // ***** This line gives a warning