Sign in to follow this  
bikola_p

Operator Overloading?

Recommended Posts

Hey, im quiet confused with this, basically i understand it is to allow say two user definend types to have workable operators, ie Having a class called Day. And creating an instance ie Day Today; Day Tomorow; Operator overloading would allow Today + Tomorow; is this correct? From what i understand i would have to do the defenition of the operator overloading in the class defenition. Day::operator++{ //increment member variables or whatever in here; return *this; } if that above is correct, i see it would work with Tody++; But how and what would i need to enable Today+Tomorow ?

Share this post


Link to post
Share on other sites
Try:


Day Day::operator+ (const Day &d) const
{
result = Variable + d.Variable
return result;
}



or something like that, its very early in the morning :)

Luke.

Share this post


Link to post
Share on other sites
I really can't understand you very well, but heres an example of operator overloading:


class CVector3
{
friend CVector3 operator+(CVector3 &rhs);

public:

...

}

...

CVector3 CVector3::operator+(CVector3 &rhs)
{
return CVector3(x + rhs.x, y + rhs.y, z + rhs.z);
}

...

int main()
{
CVector3 vec1(1.0, 1.0, 1.0);
CVector3 vec2(1.0, 1.0, 1.0);

CVector3 vec3 = vec1 + vec2;

return 0;
}


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this