How to create overloaded operators for forcing pointers in C ++?

From this post, I see that you cannot overload operators for pointers: C ++: operator overloading <for pointers to objects

But is there a way to overload operators for force pointers? For instance:

  boost::shared_ptr<ClassB> operator||(boost::shared_ptr<ClassA> lhs, boost::shared_ptr<ClassA> rhs) {
    boost::shared_ptr<ClassB> returnVal = CreateClassBSharedPtr(lhs, rhs);
    return returnVal;
  }

When I try this, I get an ambiguous overload error (it contradicted the built-in operator || (bool, bool)). Any ideas to get around this?

Edit: adding more details on why I would like to do this.

I will try to explain that I am trying my best. I would like to make a validator object for maps that can check if certain properties are saved. For instance:

boost::shared_ptr<MyValidator> my_validator = IsEmpty("key name 1") && IsNotEmpty("key name 2") || HasNElements("key name 3", num)

Later, to check the map:

if(my_validator.validate(some_map)) { ... }

, , pass by value ( ), pass by const reference ( , , ).

: , : , ++?

+3
3

, , :

#include "boost/shared_ptr.hpp"

template <class A>
bool operator||(boost::shared_ptr<A> lhs, boost::shared_ptr<A> rhs) {
    return true;
}

int main() {
    boost::shared_ptr <int> a, b;
    bool x = a || b;
}

, , , , !

+1

, , .

, rhs lhs.

0

|| bool, ( ) . , , . GetValidatedPtr() - simlar.

0

Source: https://habr.com/ru/post/1758747/


All Articles