Archived

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

CProgrammer

operator command

Recommended Posts


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