operator command

Started by
4 comments, last by CProgrammer 20 years, 7 months ago
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
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/
Abnormal behavior of abnormal brain makes me normal...
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
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;
}
Great thanks you guys.

-CProgrammer
quote:Original post by Lord Bart
Opps I forgot something in my post.

OT: You know there is a edit button so you can edit a posted message?

[How To Ask Questions|STL Programmer''s Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]

This topic is closed to new replies.

Advertisement