Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

The same question goes for | and ||.

Why would one overload or 'use' the & and && operator? The only use i thought of are

  • Bitwise Ands for int base types (but not float/decimals) using &
  • logical short circuit for bools/functions that return bool. Using the && operator usually.

I cant think of any classes that use those operators. Absolutely none. I know a class might support + (and not '-') which combine two strings together. I seen an object such as datetime overload '-' so two dates can be subtracted to make a timespan (obviously you cant add two dates) but i never seen &, &&, | and || used.

Does anyone know of a use? In any language?

share|improve this question
3  
A custom "many, many bits" type? –  user1249 Mar 6 '11 at 11:23

7 Answers 7

Basically, any class that has set semantics has a use for & - for example, sets themselves, bit vectors etc. Any class that has logic semantics has a use for && - fuzzy logic, k-ary logic, etc.

share|improve this answer

IMO it makes some sense to overload the & operators for Set types; resulting in the intersection of the two sets. Likewise for the | operator, which would result in the union of the two sets.

share|improve this answer
    
What should + be for a set type? –  acidzombie24 Mar 6 '11 at 10:29
3  
acidzombie: actually, there is no good definition of + for a set, though one might be tempted to use it for union; but obviously A+B-B=A doesn't hold (neither does A-B+B=A) –  user281377 Mar 6 '11 at 17:03
    
You could use + for a special union that keeps duplicates. Then - would be straightforward and A+B-B=A would indeed hold. Of course, you probably lose some nice set properties there. Is that what a multiset is? Hmm. –  Matthew Read Mar 6 '11 at 17:41
1  
Matthew Read: That would be a multiset, indeed, but A-B+B=A still doesn't hold, unless you allow negative multiplicities. –  user281377 Mar 6 '11 at 17:42
    
This is exactly what the Python set type does. It also uses ^ for symmetric difference. –  dan04 Mar 6 '11 at 19:27

Note:

In C++ you should not overlaod the && and || operators. The overloaded versions are not shortcut operators anymore. overloaded operators are syntactic sugar for function calls and as such all parameters are evaluated before the call.

Talking about C++:
As with all user definable overloads you can make them do anything; its totally up to the developer. Unfortunately that is also their downfall. Overloading operators to do non-standard operations confuses people and makes the code harder to maintain.

The C++ << , >> operators spring to mind. Not only are they bit shift operators but in C++ they have been overloaded to be stream input/output operators. To the new comer to C++ this usage catches them off guard (and you see the questions hear all the time).

Thus in my opinion you should only overload operators to do exactly what the user expects to be done. Thus '|', '&' should only be overloaded to do a bitwise 'or', 'and'. If this has meaning to your class then fine otherwise you should not overload them.

As for other uses I have seen the '|' operator used as a string concatenation operator.

share|improve this answer

When doing domain driven design and using the specification pattern one must override the logical operators.

Read this nice article on the CodeProject Or watch this screen cast on dimecast

The specification pattern is a great way to make domain logic explicit and more readable for business annalists.

Tip: read this great book on domain driven design

share|improve this answer
    
I looked at this and i watched the dimecast, i am completely confused. Why don't they overload the operators instead of creating many functions and calling them And/Or/Not ? en.wikipedia.org/wiki/Specification_pattern#Example_of_use –  acidzombie24 Mar 6 '11 at 13:38
    
Sorry in my memory they did. I can imagine they didn't because they wanted to create a simpeler example or because they find the methods easier to read in the fluent interface. –  KeesDijk Mar 6 '11 at 15:16

The '&' is sometimes hijacked for other purposes as well.

Example is the boost serialization library, they use it as their in-out function call instead of separate << and >> methods:

ar & data;

Can therefore be used in both seralize and deserialize calls.

share|improve this answer
    
Thats a pretty awesome use. –  acidzombie24 Mar 7 '11 at 15:08

I have at times used them to do operations over user defined classes. Makes reading the code easier.

share|improve this answer
    
Can you give me an example? this is what interest me. –  acidzombie24 Mar 6 '11 at 11:40
    
The above example of sets is one. I work with a lot of signal video manipulation and dealing with that data is much easier if you follow OOD and overload these operators while doing encoding/decoding operations. eg: frame1 & frame2 or frame1 && frame2 for comparison etc. –  Ankur Chauhan Mar 6 '11 at 14:03
    
I dont understand, why would you use frame1 '&' or '&&' frame2? what is it suppose to do? –  acidzombie24 Mar 6 '11 at 14:59

The | operator can be used for shell-like piping, when using a programming language as a "shell" language -- see IPython IPipes for an example of this use.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.