28 Sept 2019

  • September 28, 2019
  • Amitraj
Operator overloading in C++

C++ has the ability to provides the operators with a special meaning  for a data type. The mechanism of giving special  meanings to an operator is known as operator overloading.

we can overload all the C++ operators except the following:-

1.  class member access operator(. , .*)
2.  Scope resolution operator(::)
3.  sizeof operator(sizeof)
4.  conditional operator(?:)


Important points about operator overloading
1) For operator overloading to work, at least one of the operands must be a user defined class object.

2) Assignment Operator: Compiler automatically creates a default assignment operator with every class. The default assignment operator does assign all members of right side to the left side and works fine most of the cases (this behavior is same as copy constructor). See this for more details.

3) Conversion Operator: We can also write conversion operators that can be used to convert one type to another type.


4) Any constructor that can be called with a single argument works as a conversion constructor, means it can also be used for implicit conversion to the class being constructed.



Translate

Popular Posts