Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

CProgrammer

operator command

This topic is 5410 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Could someone give me an example of the operator command to add to calsses, so operator+ on this struct: struct my_struct { float a, b, c; }; -CProgrammer

Share this post


Link to post
Share on other sites
Advertisement

class WTF
{
public:
WTF operator + (const WTF&);
private:
int a,b,c;
};

WTF WTF::operator + (const WTF& lRhs)
{
WTF temp;
if(this != &lRhs)
{
temp.a = a + lRhs.a;
temp.b = a + lRhs.b;
temp.c = a + lRhs.c;
}
return(temp);
}


there are some cool articles here:
http://www.brent.worden.org/articles/

Share this post


Link to post
Share on other sites
Hello CProgrammer,

struct my_struct
{
float a, b, c;
};

class test
{
float one, two, three;
public:
operator+ (const struct my_struct& in)
{
test ret;
ret.one = in.a;
ret.two = in.b;
ret.three = in.c;
return ret;
}
}

This adds operator+ to a class to accept my_struct as something to be added to a test class. You need to have a local temp test object created and return. it also need to be public so as to be used form outside the class. since this is so small you could also inline the ooperator+.

Lord Bart

Share this post


Link to post
Share on other sites
Opps I forgot something in my post.

operator+ (const struct my_struct& in)
{
test ret;
ret.one = in.a;
ret.two = in.b;
ret.three = in.c;
return ret;
}

should have been
operator+ (const struct my_struct& in)
{
test ret;
ret.one = *this.one + in.a;
ret.two = *this.two + in.b;
ret.three = *this.three + in.c;
return ret;
}

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!