+ 1

please Could someone Give me an accurate explanation....?

class MyClass { public: int var; MyClass() {} MyClass(int a) : var(a) { } MyClass operator+(MyClass &obj) { } }; for the code above is it possible to write Void operator+(MyClass &obj) instead of MyClass operator+(MyClass &obj)...? i mean why we used the Class Name "MyClass" as the Return type of our operator function?? is it always Necessary to use the class name or we can also Use Void ??

30th Apr 2018, 8:36 AM
RiGeL
RiGeL - avatar
2 Answers
+ 2
you can use void but you most of the time want to use class type because most of the time that's how you want it to work look at following example think your class is vector(I mean a vector in maths not std::vector) and you wanna overload + operator what you will do is vector result = veca + vecb; you will need the result unless you're storing the result in one of vectors. if you don't need the result use void.
30th Apr 2018, 9:51 AM
Ias0601
Ias0601 - avatar
+ 1
IAS2000 Thank you alot bro
30th Apr 2018, 9:54 AM
RiGeL
RiGeL - avatar