Skip to main content
GameDev.net gamedev.net
🔒 Locked

operator command

Started by CProgrammer Sep 2, 2003 at 5:12 AM 4 replies 350+ views
Original Post
CProgrammer
CProgrammer
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
original vesoljc
original vesoljc

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/
Abnormal behavior of abnormal brain makes me normal...
Lord Bart
Lord Bart
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
Lord Bart
Lord Bart
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;
}
CProgrammer
CProgrammer
Great thanks you guys.

-CProgrammer

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.