operator command
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
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/
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
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;
}
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;
}
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]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement