answersLogoWhite

0

What is meant when you say you overload an operator?

Updated: 8/21/2019
User Avatar

RichardIsaacsgp7883

Lvl 1
9y ago

Best Answer

All operators are built-in but not all operators can operate upon data types they know absolutely nothing about. There are some exceptions such as the new operator and the sizeof operator -- both will work on any datatype. However, for those that cannot, operator overloads allow you to cater specifically for those types.

An operator overload is implemented just as you would overload a function, but it is not a function per se because operators have different calling conventions to functions. It is also important to keep in mind that just because you can overload an operator, it does not mean that you should. Operators should only be overloaded when the overload would allow a user to interact with your data type in an intuitive manner; a manner that is consistent with the operator's intended purpose. So while it can be amusing to overload the plus (+) operator to perform a subtraction (-), it could hardly be called intuitive.

The assignment operator is the most overloaded operator of them all. This is because it is extremely useful to be able to copy the members of one object and assign those values to another object of the same type. We can also overload the assignment operator to cater for objects of different types, but such assignments are rarely intuitive. Does it make sense to assign the properties of a banana object to a person object? Probably not. Even if you could find a practical reason for doing so, would it be an intuitive operation? Definitely not. Therefore there's no point in providing an operator to cater for this.

To create an operator overload, the declaration will often be placed inside the class it pertains to. However there are exceptions. The output stream insertion operator is a good example of this. The following example demonstrates how we can overload an internal operator (the assignment operator) as well as an external operator (output stream insertion operator).

#include<iostream> // required to make use of I/O streams

class A

{

private:

unsigned m_data;

public:

// constructors...

A (const unsigned data = 0): m_data (data) {}

A (const A& copy): m_data (copy.m_data) {}

// accessor function (interface)

unsigned get_data() const { return m_data; }

// operator overloads...

A& operator= (const A& rhs) { m_data = rhs.m_data; }

A& operator= (const unsigned rhs) { m_data = rhs; }

};

std::ostream& operator<< (std::ostream& os, const A& a

{

os << a.get_data();

return os;

}

int main()

{

A a, b; // invoke default constructors

a = 42; // call assignment operator overload

b = a; // call default assignment operator overload

// call insertion operator overload

std::cout << a << std::endl;

std::cout << b << std::endl;

}

Output:

42

42

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is meant when you say you overload an operator?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the operator that cannot be overloaded in c plus plus and java?

conditional operator , size of operator , membership operator and scope resulation operator can not be overload in c++


How unary minus can be overload in c plus plus?

type operator- ();


What is the declaration of the function to overload output operator inside class in c plus plus?

You can't overload the insertion operator (&lt;&lt;) from inside a class because the l-value must be an ostream object, but operator overloads implemented within classes always implicate the class instance itself as being the l-value. You must overload the insertion operator from outside of the class, like so: ostream&amp; operator&lt;&lt;(ostream&amp; lhs, const MyObject&amp; rhs) { lhs &lt;&lt; rhs.get_data(); return( lhs ); }


Difference between new operator and operator new?

new operator allows to allocate a memory from the heap..... so a new instance of a class is created.......... but operator new is used to overload the (new) operator........ juast like overloading of other operators


Is sizeof an operator or function why?

You cannot overload the sizeof() operator because that could introduce uncertainty in its evaluation. The sizeof() operator must always produce an accurate and logically predictable result, thus all user-intervention is completely forbidden.


Why should you have to use comparison operator overloading in pairs in c?

You cannot overload operators in C. This is a C++ thing only.


What is meant by fundamental overload in reference to a receiver?

Fundamental Overload refers to the receiver picking up a source so powerful it causes overload. IE, the receiver is picking up the fundamental output of the functioning transmitter, but is overpowered by it.


Write c plus plus code to overload operator to find maximum between two objects?

The only operator you need to overload is the greater-than operator. You can then use the std::max function from the standard template library. #include&lt;iostream&gt; #include&lt;algorithm&gt; // for std::max() class my_object { private: int m_data; public: my_object(int data=0): m_data(data) {} my_object(const my_object&amp; copy): m_data(copy.m_data) {} my_object&amp; operator=(const my_object&amp; other) { m_data = other.m_data; } my_object&amp; operator=(const int) { m_data = int; } // greater-than operator overload: const bool my_object::operator&gt; (const my_object&amp; other) { return this-&gt;m_data &gt; other.m_data; } }; int main() { my_object a = 42; my_object b = 0; my_object c = std::max(a, b); // alternatively: my_object d = a &gt; b ? a : b; } Note that the alternative method shown above is slightly less obvious than calling std::max, but both do exactly the same thing. However, both methods require you to overload the greater-than operator.


What is meant by cast operator?

Casting operator used in implicite conversion.Because,it reduce the data lossing in imolicit conversion.


What is operator function?

An operator function implements a particular operator symbol. The database server provides special SQL-invoked functions, called operator functions, that implement operators. An operator function processes one to three arguments and returns a value. When an SQL statement contains an operator, the database server automatically invokes the associated operator function. The association between an operator and an operator function is called operator binding. You can overload an operator function to provide the operator for a UDT. The SQL user can then use the operator with the UDT as well as with the built-in data types. When an SQL statement contains an operator, the database server automatically invokes the associated operator function.


How can you can create a new operator through operator overloading?

You cannot create any new operators in C++. You can only overload the existing ones (although some, such as sizeof, new and delete cannot be overloaded). The only way to create a new operator is to implement it as a standard function with a named identifier. For instance, sqrt() is the standard library function that provides the square root operator, for which no real operator exists.


How might one determine if he or she suffers from technology overload?

Well... go with the flow, I guess. Try not to use to much but don't completely shove it out of your life. Use it for what its meant to be used for.