Is there any way to use operator overloading in dynamic C++ object
I want to use operator overloading with dynamic object in C++
how can i do it?
I think by dynamic object you mean objects created to heap, so they are referenced as pointers. You are right, using overloaded operators for such objects is not as comfortable as local objects. But it is possible. And it looks terrible. And because we are talking about C++, there's more than one way to do it.
Let's say we have a class called Irrational, which overloads the += operator, you can do:
Irrational* a = new Irrational(2,-4); a->operator+=(3); Irrational* b = new Irrational(2,-4); (*b) += 3;
So a heap object's overloaded operator is called either by calling it with the "operator" prefix, in which case it is a lot like a function call, or by converting the pointer first to a reference, in which case it works more "normally". That's it.