Learning About Operator Overloading

Started by
5 comments, last by nmi 17 years, 6 months ago
OK, Im going through one of my C++ books and have came upto a chapter about operator overloading. So I loaded up my chosen IDE and got coding. Heres what I got so far:

////////////////////////////////////////////////////////////////////////////////
// Operator Overloading
// Matthew Henley
// 19/09/2006
////////////////////////////////////////////////////////////////////////////////
#include <iostream>
using namespace std;

class variable
{
public:
        variable(): Val(0) {};
        
        const variable& operator =(int val);

private:
        int Val;
};

const variable& variable::operator =(int val)
{
        Val = val;
        return *this;
}

int main()
{
        variable a,b,c;
        a = 5;
        b = 10;
        c = 20;
        
        cout << "A = " << a << endl;
        cout << "B = " << b << endl;
        cout << "C = " << c << endl;
        
        system("pause");
}


As you can see this code wont compile because of these lines here:

cout << "A = " << a << endl;
cout << "B = " << b << endl;
cout << "C = " << c << endl;


Is it possible to do what im trying to do like this or will I have to create a function in my variable class that returns Val. Thanks again.
Advertisement
You need an overloaded << function but you also need an accessor to get the value in this function to save making the << function a friend (which sucks for a simple class like this)

#include <iostream>using namespace std;class variable{public:        variable(): Val(0) {};                const variable& operator =(int val);        int GetValue() const { return Val; }private:        int Val;};const variable& variable::operator =(int val){        Val = val;        return *this;}ostream &operator<<(ostream &os,const variable &v){    return os << v.GetValue();}int main(){        variable a,b,c;        a = 5;        b = 10;        c = 20;                cout << "A = " << a << endl;        cout << "B = " << b << endl;        cout << "C = " << c << endl;                system("pause");}


Simple as that.
Wow thanks for the fast reply man, I was hoping to go to collage and by time I get back there might be a 1 or 2 posts lol. Thats exactly what i was looking for too man. Cheers.

Rating++;
Just for completeness (I was editing my last post when you replied but the server bombed out on me)...

The friend alternative, which could be useful if you have a more complex class and don't want to provide a load of accessor functions would look like this:

#include <iostream>using namespace std;class variable{public:        variable(): Val(0) {};                const variable& operator =(int val);private:        friend ostream &operator<<(ostream &os,const variable &v);        int Val;};const variable& variable::operator =(int val){        Val = val;        return *this;}ostream &operator<<(ostream &os,const variable &v){    return os << Val;}int main(){        variable a,b,c;        a = 5;        b = 10;        c = 20;                cout << "A = " << a << endl;        cout << "B = " << b << endl;        cout << "C = " << c << endl;                system("pause");}


That is certainly one of the less evil uses of a friend function although generally you would want to provide accessors for other uses anyway and it is best to use the first approach unless it is impractical or compromises your encapsulation in some way.

And since I am in a very good mood indeed today, I won't criticise your use of system("pause"); [smile]. I would normally though since it is not good practice.
Whilst im studying this I might aswell ask.

What are the uses if any of operator overloading in game development?

Thanks yet agin :P
Far too numerous to mention, but much the same as in any other environment really. I don't see much of a difference between games and anything else in this respect.

The game I'm writing at the moment overloads ofstream<< for the output of some data types to a log file, I overload () on my map class to get a cell via x,y co-ordinates, the list goes on.

The only things to be aware of are - don't overuse operator overloading where a normal function would be more appropriate (If you're like me, you'll go a bit mad with operators when you first get the hang of them but you end up looking back at old code and shaking your head a bit ruefully) and NEVER EVER overload an operator to have a different meaning to its meaning with built in types since that is very very bad indeed.

I've read that the reason they left operator overloading out of Java was because they felt people had abused it to much in C++. Don't know if that is a true story or not.
Maybe you will find this interesting:
http://www.parashift.com/c++-faq-lite/operator-overloading.html

This topic is closed to new replies.

Advertisement