3

From this post I see that you can't overload operators for pointers: C++: Operator overloading of < for pointers to objects

But is there any way I could overload operators for boost pointers? For example:

  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 attempting this, I get an ambiguous overload error (it conflicted with the built in operator||(bool, bool)). Any ideas to get around this?

Edit: Adding some more details below as to why I'd like to do this.

I'll try to explain what I'm attempting as best I can. What I'd like to do is make a "validator" object for maps that can check if certain properties hold. For example:

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

Later, to validate a map:

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

I think I'm stuck with using pointers because I can't use pass by value (since I'm making use of polymorphism) and I can't use pass by const reference (since there would be temporary object created by nesting operators that would not exist later when trying to validate).

Edit: Added a new question specific to my problem here: Implementation suggestions for creating an expression later used to evaluate the contents of a map in c++?

Community
  • 1
  • 1
md23
  • 51
  • 5
  • 2
    not that it answers your question, but... **why would you do this**?!? – Evan Teran Aug 09 '10 at 14:30
  • Following Evan's comment: http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.8 – Patrick Aug 09 '10 at 14:37
  • 1
    So you really want to construct an expression, and then later apply that expression to the contents of a map? If so, I would ask another, different question about that, leaving out the shared pointer stuff, which seems to be an implementation detail. –  Aug 09 '10 at 14:51
  • In other words, never ask about steps but about problems. – GManNickG Aug 09 '10 at 15:02
  • Yes, I understand your recommendations. I was curious about this issue in any case, thanks for your comments. – md23 Aug 09 '10 at 15:14

3 Answers3

1

You certainly can:

#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;
}

Whether you can do what you are doing in your example, I don't know, as I'm not sure what you are trying to do!

0

Your approach should work, at least it works for me.

However, you will always run into the problems with ambiguous overloads when trying some user-defined conversions on rhs and lhs.

jpalecek
  • 47,058
  • 7
  • 102
  • 144
0

Operator|| must always return a bool, so you can't (and for the sake of the sanity of your co-workers, shouldn't) do that. I suggest you use a regular function and give it a name that reflects what you are trying to do. GetValidatedPtr() or something simlar.

Alan
  • 4,897
  • 2
  • 24
  • 17
  • There is nothing that says operator || *must* return a bool, but of course it is good practice for it to do so. –  Aug 09 '10 at 15:46
  • Well, if the standard doesn't demand it, then I personally demand it! – Alan Aug 09 '10 at 18:12