overloading in C++
-
hello everybody : anyway i am working on program that use alot of object if i want to compare in (if statement) like
if(ob.x==ob1.x)
if(ob.x != ob1.x)do i need to overload these operators or not (because visual studio says no operator match , if yes could you tell me the code with simple explain thanks anyway .
-
hello everybody : anyway i am working on program that use alot of object if i want to compare in (if statement) like
if(ob.x==ob1.x)
if(ob.x != ob1.x)do i need to overload these operators or not (because visual studio says no operator match , if yes could you tell me the code with simple explain thanks anyway .
if the compiler says there is no operator, then yes, you have to overload == http://courses.cms.caltech.edu/cs11/material/cpp/donnie/cpp-ops.html[^]
-
hello everybody : anyway i am working on program that use alot of object if i want to compare in (if statement) like
if(ob.x==ob1.x)
if(ob.x != ob1.x)do i need to overload these operators or not (because visual studio says no operator match , if yes could you tell me the code with simple explain thanks anyway .
Assuming
x
is an object of typeX
, you will need to overload the==
and!=
operators inclass X
.class X
{
bool operator ==(const X& rhs)
{
return this.var == rhs.var ? true : false; // Assuming var is a member variable of fundamental type like int.
}bool operator !=(const X& rhs) { return this.var == rhs.var ? false : true; // Assuming var is a member variable of fundamental type like int. }
};
The comparison logic would really depend on your particular business case.
«_Superman_» _I love work. It gives me something to do between weekends.
-
Assuming
x
is an object of typeX
, you will need to overload the==
and!=
operators inclass X
.class X
{
bool operator ==(const X& rhs)
{
return this.var == rhs.var ? true : false; // Assuming var is a member variable of fundamental type like int.
}bool operator !=(const X& rhs) { return this.var == rhs.var ? false : true; // Assuming var is a member variable of fundamental type like int. }
};
The comparison logic would really depend on your particular business case.
«_Superman_» _I love work. It gives me something to do between weekends.
-
struct
andclass
are the same in C++. Only difference is that, by default, class members areprivate
and struct members arepublic
.«_Superman_» _I love work. It gives me something to do between weekends.
-
struct
andclass
are the same in C++. Only difference is that, by default, class members areprivate
and struct members arepublic
.«_Superman_» _I love work. It gives me something to do between weekends.
but there is problem :
this .x
x is a member in struct not in class , and the hole struct is a private member of class and the overload i need it for object of struct , not the class, so that compiler tel me that x is not a member of class, ( error class y has no member x,
-
but there is problem :
this .x
x is a member in struct not in class , and the hole struct is a private member of class and the overload i need it for object of struct , not the class, so that compiler tel me that x is not a member of class, ( error class y has no member x,
Overload the operators inside the struct.
«_Superman_» _I love work. It gives me something to do between weekends.