c++11 - What all operators can be defined inside a struct in C++? -
this question has answer here:
i cannot understand 3rd,4th line in following code in c++ :
struct point { int x,y; point(int x = 0, int y = 0) : x(x), y(y) {} bool operator ==(point& a) {return a.x==x && a.y==y;} }; other examples/explanations welcome apart code snippet :)
structure nothing class data members , functions defined in public scope. can implement operators valid class.
if question on operator overloading, overloaded operators can defined class valid structure.
you can refer operator overloading question , answer understand more on operator overloading
point(int x = 0, int y = 0) : x(x), y(y) {} //(3rd line) bool operator ==(point& a) {return a.x==x && a.y==y;}//(4th line) 3rd line constructor, , 4th line operator overloading function operator ==.
point p1(2,3); // 3rd line called , update x=2 , y=3
point p2; // 3rd line called , consider x=0 , y=0 //since function definitiion defaulted value x=0 , y=0; if(p2==p1) {....} // 4th line called , compare p1 , p2
Comments
Post a Comment