CSIT Object Oriented Programming with C++(Rules for Operator Overloading ) Part II
I am assuming that, You have already studied Part I of Operator Overloading Series. So in this post, I am going to teach you about Rules That should be followed while performing operator Overloading.
So, let's get started,
The following Rules must be followed while doing operator overloading in C++.
- Operators that are already defined in the C++ compiler can be only overloaded. Operator can't change operator templates that is for example, the decrement operator '--' is used only as unary operator. It can not be used as binary operator.
- Overloading an operator does not change its basic meaning. for example assume the + operator can be overloaded to subtract two objects. But, still when we use it with two numbers, it finds sum of two numbers.
- Unary operators, overloaded by means of a member function, takes no explicit argument and return no explicit values. But, those overloaded by means of a friend function takes one reference argument(the object of relevant class).
- Binary operator overloaded through a member function take one explicit argument and those which are overloaded through a friend function take two explicit arguments.
- Overloaded operators must either be a non-static class member function or a global function.
- A global function that needs access to private or protected class members must be declared as a friend function of that class. A global function must take at least one argument that is of class or enumerated type or that is a reference to a class or enumerated type.
In next post, we will learn How to overload Unary operators. So refer next post for that topic.
If This post Helped you, Please leave a comment, and share with your Coder Friends.
Thank You.
Keep Learning, Keep Growing
Comments
Post a Comment