c++ - Is there a smart way to inhibit hiding a operator function of base class by one of derived class -
i learning c++ while. make pointer class curiously recursive template pattern, following. unfortunately, did not work because default operator==() function of derived class hides operator==() function of base class. have smart way inhibit hiding operator function of base class 1 of derived class? or know way avoid issue? in case, append messages compiler. thank much.
#include <vector> template <typename derived, typename t> class function0 { public: friend bool operator== (derived& org, derived& cmp) { // ***** line gives 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; // ***** line gives 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) { (unsigned int = 0; < vec.size(); ++i) { auto o = vec[i]; if (obj == o) { // dispatches 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; }
a part of messages gcc compiler following,
/home/.../pointer.hpp: in instantiation of ‘element findcorrespondingfirst(element&, std::vector<_realtype>&) [with element = pointer<testa, function0>]’: /home/.../pointer.hpp:120:56: required here /home/.../pointer.hpp:68:11: error: ambiguous overload ‘operator==’ (operand types ‘pointer<testa, function0>’ , ‘pointer<testa, function0>’) if (obj == o) { // dispatches 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; // ***** line gives warning ^ /home/.../pointer.hpp:23:14: note: bool operator==(pointer<testa, function0>&, pointer<testa, function0>&) friend bool operator== (derived& org, derived& cmp) { // ***** line gives warning ^
thank again.
the line
bool operator== (pointer& cmp) = delete; // ***** line gives warning
is wrong in multiple ways. in fact, erasing removes problem (and should consider doing that, unclear purpose serves, must say).
incidentally, correct way have defined declare as
bool operator== (const pointer<t, functions>& cmp) const = delete; // ***** line gives warning
note class type not pointer
, rather pointer<t, functions>
, , correct const
ness properties of operator.
your first operator==
has const problems too.
i suggest make following changes:
friend bool operator== (const derived& org, const derived& cmp) { // ***** line gives warning const derived& org_value = static_cast<const derived&>(org); const derived& cmp_value = static_cast<const derived&>(cmp); return org_value == cmp_value; }
just erase latter one:
// bool operator== (const pointer<t, functions>& cmp) const = delete; // ***** line gives warning
Comments
Post a Comment