+ 2
Hi, please help me with operator overloading in C++. Thank you=)
In my below code, I have mentioned a comment line inside main function. My doubt is, why is both working? Is the compiler doing any implicit conversion there (like in the case of pass by reference(not by address))? I know operator overloading, just have to understand how it works. Have a nice day=) https://code.sololearn.com/cABCNIwPqi03/?ref=app
3 ответов
+ 1
When C++ reads
A + B
It sees it as
add(A, B)
It now searches if there is a definition of 'operator+' that has parameters of those types
And uses that to perform the command
So they both work becouse they say the same thing
+ 1
Angelo wow, I just got the answer I guess. Just one more thing. Is there another format for the same function? Like, "MyClass res=operator+(&obj1,obj2)" which also does the same job?
+ 1
Kind of... While C++ uses samething similar to MyClass::operator+(&obj1, obj2) to perform the job,
That function doesn't exist for you
But, if you follow the common way to declare operators, than operator+ would be friend and not a member function, so there would actually be MyClass::operator+