Posted by AcidBurn on 01:55:00 02-21-2002
Ok I just wrote a VERY small app in C++ to test out something on operator overloading and for some reason my source codes seems to look sloppy. Can you look at this and tell me if there is a better way or not thx
#include ()
class OOV {
public:
virtual OOV operator+ (OOV);
void SetData(int x){ Data=x;};
int GetData() { return Data;};
private:
int Data;
};
OOV OOV::operator+ (OOV x){
OOV temp = *this;
int v=x.GetData() + temp.GetData();
temp.SetData(v);
return (temp);
}
void main(){
OOV ad;
OOV ab;
ad.SetData(2);
ab.SetData(5);
OOV c;
c = ad+ab;
cout
};
[ This Message was edited by: AcidBurn on 2002-02-21 02:02 ]
Posted by AcidBurn on 01:56:00 02-21-2002
Btw the above code works and the output is 7 like I wanted I just wanna know if there is a better way to go about this.
[addsig]
Posted by fsvara on 17:41:00 02-21-2002
btw, if you don't want stuff like to be interpreted as html, check the "disable html" checkbox when posting.
Posted by SilentStrike on 22:16:00 02-21-2002
You don't need to call the getData and setData member functions inside the operator + implementation, as the classes own implementation has access to the private variables, but that's fine nontheless. You could also provide a constructor that takes and integer, and initialize in the constructor, rather than creating an instance with the default constructor and using the setData method. Regardless, if it works, it works.
Posted by AcidBurn on 22:52:00 02-21-2002
Thx a bunch... at least here people help ya out when ya need it.
[addsig]
Posted by neonbjb on 02:37:00 12-21-2002
You dont have to use such an obscure name as OOV, that just makes the code more confusing and the this pointer is constant, you dont need to assign a variable to it(added on to the above)
Posted by KaGez on 13:29:00 12-21-2002
as long as he can get along with it, I think we don't have to care about his variable names
[addsig]