有没有一种聪明的方法可以禁止通过派生类之一隐藏基类的运算符函数
Is there a smart way to inhibit hiding a operator function of base class by one of derived class
我正在学习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
相关文章:
- 从多个模板化基类派生时出现"隐藏重载的虚函数"警告
- 从基调用派生类的隐藏(非虚拟)方法
- static_assert:派生"must"中的某个函数隐藏了Base的类函数
- 基的私有虚函数由派生的私有虚函数隐藏
- 编译器不应该对派生类中隐藏的基本结构的成员变量发出警告吗?
- 在派生类接口中隐藏基类的特定函数
- 为什么在派生隐藏基函数时使用虚函数
- 为什么基类函数没有被同名的派生类函数隐藏
- 在派生类中使用声明并不能隐藏从基类派生的相同函数
- 隐藏在派生类中的虚函数
- 有没有一种聪明的方法可以禁止通过派生类之一隐藏基类的运算符函数
- 隐藏代码以选择接口类的派生类
- 运算符=(double)隐藏在派生类中
- 取消隐藏派生中基类中具有相同名称和不同签名的某些函数
- 如何通过派生类实现等效的类型隐藏
- 多态性和数据隐藏:基类是覆盖还是忽略派生类的访问限制?
- 基类模板成员函数隐藏在派生类中,尽管参数列表不同
- 隐藏在派生类中的成员变量
- CLR 中的派生类是否可以拦截/隐藏父类的方法,同时仍保持智能感知?
- 为什么派生类重载函数隐藏基类函数