object - C++ operator Overloading the is equal operator -


i'm trying learn operator overloading in c++.

i have followed tutorial , overloaded '==' operator able compare objects of class between them. this:

bool vector2::operator==(const vector2& v) const {     return ( (x_==v.getx()) && (y_==v.gety()) ); } 

my class has 2 vars in (x_ , y_). have created 2 objects class gave values , compared them , worked fine. used == operator compare integers , worked fine. expecting == operator not work on integers because had overloaded else. still did. i'm little confused right now. , have question:

how comes == operator after beiing overloaded can still used compare integers/floats/doubles/etc? in case when sees vector2 class object second parameter acts in overloaded version , when sees else acts normal?

also question: have class classa , class classb both 2 variables in them , different in name. create 2 objects 1 classa obja , 1 classb objb , give values default constructor.

now possible overload operator == comparasion if(obja==objb)? or operands in expression need of same type? either classa or classb?

please me clear up. thank reading!

if vector2 class has constructor takes int , not marked explicit, allows integers implicitly converted vector2 shown in example:

#include <iostream> #include <assert.h>  struct s {     s(int x) : x(x) {}      bool operator==(const s& other) const     {         return x == other.x;     }     int x; };  int main() {     s s1{5};     s s2{5};      assert(s1 == s2);      assert(!(s1 == 5));      return 0; } 

ideone

this code compiles, second assertion fails, because 5 converted s.


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -