Subscribe to while(1) { try(); }        RSS Feed
-----

Operator Overloading

Icon 5 Comments
Note: I am speaking in the context of C++, I know very little of OO in other langs.

I was recently discussing operator overloading with a friend of mine and it brought about a fair share of contention. I love to use it, mostly in the context of math. Most of the programming I do is in the context
of OpenGL lately, so having this type of short hand functionality really streamlines the process.The general concern I continually hear is that people can abuse it. While this is true, people can also choose to name
their variables a, a1, a2, ..., a99. If used within reason, I feel that they can clarify code immensely.

Here is an example from a library I'm working on:


template <class generic>
class vec3
{
public:
	vec3 operator=(const vec3<generic>& rhs)
	{
		this->x = rhs.x;
		this->y = rhs.y;
		this->z = rhs.z;
		return *this;
	}

	generic x;
	generic y;
	generic z;
};


#include <iostream>

int main()
{
	gfx::math::vec3<float> foo;
	foo.x = 3; foo.y = 4; foo.z = 5;

	gfx::math::vec3<float> bar;
	bar.x = 7; bar.y = 7; bar.z = 7;

	std::cout << bar.x << '|' << bar.y << '|' << bar.z << std::endl;
	std::cout << foo.x << '|' << foo.y << '|' << foo.z << std::endl;
	
	bar = foo;
	std::cout << bar.x << '|' << bar.y << '|' << bar.z << std::endl;

	return 0;
}



Here are a few guidelines I have developed for using OO in C++:

1. Use an operator overload to promote clarity

YES
 
Matrix3 a, b;

Matrix3 c = a*b;



NO
Elephant a, b;

a \= b; //Does elephant a eat b?



2. Use them for operations which are often strung together, such as arithmetic.

3. Make sure the work and risk of creating an OO outweighs its usage. If you end up
using it only a few times, you may be better of creating a few methods.

So, do we need operator overloading? No. Can we get by just fine without it? Of course.
I can get by without egregious amounts of chocolate milk in my fridge, but I don't have to.

Let me know what you guys think. Maybe I'm missing the big picture, be sure to let me know.

5 Comments On This Entry

Page 1 of 1

BBeck Icon

05 September 2013 - 12:52 PM
Are you certain that you can get by without egregious amounts of chocolate milk? :-)

I'm with you. It really annoys me when I see math without operators like mul(a,B). It's one thing if you're in Assembly Language, but there's a reason we use high level languages instead.

Matrix3 c = MultMat(a,B)/>;



Just feels wrong to me. I guess it's what you get used to, but seeing a multiplication operator seems to make it more readable to me.
0

BBeck Icon

05 September 2013 - 12:53 PM
Are you certain that you can get by without egregious amounts of chocolate milk? :-)

I'm with you. It really annoys me when I see math without operators like mul(a,B). It's one thing if you're in Assembly Language, but there's a reason we use high level languages instead.

Matrix3 c = MultMat(a, b );



Just feels wrong to me. I guess it's what you get used to, but seeing a multiplication operator seems to make it more readable to me.
0

BBeck Icon

05 September 2013 - 12:54 PM
Are you certain that you can get by without egregious amounts of chocolate milk? :-)

I'm with you. It really annoys me when I see math without operators like
mul(a,B)/>
. It's one thing if you're in Assembly Language, but there's a reason we use high level languages instead.

Matrix3 c = MultMat(a, b );



Just feels wrong to me. I guess it's what you get used to, but seeing a multiplication operator seems to make it more readable to me.
0

BBeck Icon

05 September 2013 - 12:55 PM
Are you certain that you can get by without egregious amounts of chocolate milk? :-)

I'm with you. It really annoys me when I see math without operators like
mul(a,B)/>
.

It's one thing if you're in Assembly Language, but there's a reason we use high level languages instead.

Matrix3 c = MultMat(a, b );



Just feels wrong to me. I guess it's what you get used to, but seeing a multiplication operator seems to make it more readable to me.
0

BBeck Icon

05 September 2013 - 12:57 PM
Sorry about the multiple posts. Seems like it doesn't know what "edit" means. I didn't realize it was making a new post every time.
0
Page 1 of 1

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

October 2014

S M T W T F S
   1234
567891011
12131415161718
1920 21 22232425
262728293031 

Tags

    Recent Entries

    Recent Comments

    Search My Blog

    0 user(s) viewing

    0 Guests
    0 member(s)
    0 anonymous member(s)

    Categories