tel me the difference between binary and unary operator

Dani AI

Generated

The thread already covers the basics (thanks to for the unary/binary pointer and for the pre/post increment hint). Below is a compact, practical summary of the important rules and idioms that are missing from the replies—which operators you cannot change, which must be members, common implementation patterns, and a few gotchas to avoid.

Some operators simply cannot be redefined (they are part of the language syntax), and you cannot change operator precedence or the number of operands. Also, certain built-ins (for example, the scope-resolution and member-access operators and the conditional operator) are not overloadable; operator-> has special return requirements; overloaded logical operators lose short‑circuit behavior. See the standard summary at cppreference for the exact list and restrictions. (en.cppreference.com)
(Reference: cppreference — Operator overloading.)

Which form to use and canonical patterns: some operators must be members (assignment, subscript, function-call, and member-access arrow), while others are normally implemented as non‑members for symmetry (arithmetic, comparisons, stream insertion/extraction). A common, safe idiom is to implement the compound-assignment (e.g., operator+=) as a member and implement the binary operator+ in terms of it so you get correctness and efficient chaining. Example pattern:

X& X::operator+=(const X& rhs) { /* modify *this */ return *this; }

friend X operator+(X lhs, const X& rhs) { lhs += rhs; return lhs; }

cppreference documents these canonical forms and the stream <</>> pattern (the stream operators are non‑members and often declared friend). (en.cppreference.com)

Style and safety notes: keep operator semantics conventional (don’t make + do subtraction), prefer non‑member functions for symmetric operators, and only make functions friend when you truly need access to internals. Overloading &&, ||, ,, or & can change evaluation/ordering behavior or require workarounds (use std::addressof where appropriate). The C++ Core Guidelines capture these idioms succinctly; follow them to avoid surprising or brittle operator code. (isocpp.github.io)

Additional reading: the cppreference page above is a concise checklist (restrictions, canonical implementations), and the C++ Core Guidelines give the practical “how and why” for choosing member vs non‑member and keeping semantics intuitive. (en.cppreference.com)

Recommended Answers

All 7 Replies

Binary operations are operations between two variables (like a+b, a-b, a^b, a/b etc.).
Unary operations are operations on one variable (a++, a-- ...)

how to overload post and preincrement operators ?

Hi, operator++(int) and operator++() are the post and pre increment signatures respectively.

Note that the (int) in the post-increment does not get assigned or used
i.e. you might write

class A
{
  public:

   A& operator++(int) 
     { 
      // do stuff here; 
      return *this;
     }
};

Note: You do not need to return a reference to the class, you can return anything you want.

Can I strongly recommend getting a basic c++ book (see the FAQ on this section).

how to overload bitwise i.e << and >> operators ?

how to overload bitwise i.e << and >> operators ?

The same way you overload any other binary operator. There's no magic to the shift operators, though if you're not careful then precedence rules might bite you in the ass when you use them.

Look at this list of C++ operators, it shows all the operators (binary and unary) in C++ along with the prototypes for the member and non-member version of the operator overload functions. That might be a quicker reference for you than asking us for each operator.

someone please define operator overloading deeply,how it works which operators are used and which are not in 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, networking, learning, and sharing knowledge.