I'm making a class for boolean expressions, a simpler version of bool.
I need three operators, one for AND, one for OR and one for NOT.
I can easily make + for OR, by overloading, but how can i declare . for AND and ' for NOT?
Can i even do this?
Thanks in advance
P.S, i'm a beginner :)

Recommended Answers

All 3 Replies

No, you can't do this - . and ' aren't overloadable operators. You can overload & for AND and ! for NOT though (and make OR | while you're at it). That way the operators are also consistent with those of the builtin bool type.

PS: How exactly do you want to make your type simpler than bool? What's complicated about the existing bool type?

Thankyou for your reply.

Nothing complicated. I thought, instead of writing: (!A && !B) || (B && C), i could just write (A'.B')+(B.C)

Operator overloading is a mechanism where you can allow existing operators to be used with your class. You can not add new symbols to be operators, nor change their precidence (order of operations), nor number of parametres. I would love to be able to assign ^ to a higher precidence and use it for exponents, but it is stuck where it is. Most of the existing operators can be overloaded, but some operators can not be overloaded because they 1) are used to define the structure of the class, like ., .*** or **::; 2) require extra parametres to work, like ? :; 3) or has multiple syntaxes with a predefined meaning that makes no sense to change i.e. sizeof.

When looking at your example, I would be looking at using standard logical reductions to make the expression simpler.
(!A && !B) || (B && C)
Apply deMorgan's rule: The intersection (and) of two values is the negation of the union (or) of the negation of the two values.
= !(A || B) || (B && C)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.