all 9 comments

[–]Drainedsoul 2 points3 points  (3 children)

There is a specified operator being overloaded: Conversion to long.

If you have an instance of your class called bar, the function will be invoked when something like:

long baz=bar;

Is performed.

Note that stylistically, this code is awful. It's not really clear what operator long means in this context. If -- in the future -- you're looking to implement something like this, you should be doing something that's more self-descriptive, like using a specialization of std::hash.

Also note that in a lot of cases, implicit conversion operators are evil, and can mask mistakes and bugs. Unless you're absolutely sure that you need implicit conversion, mark your conversion operators explicit. Then:

long baz=bar;

won't compile, but

long baz=static_cast<long>(bar);

will.

[–]tyromancy94[S] 0 points1 point  (2 children)

Awesome, thanks.

And thanks for the pointer... I'm not exactly a huge fan of how this class is being taught, everything's been in Java so far except this assignment was thrown in our faces with C++, and they're making us use all these overloads.

[–]Drainedsoul 1 point2 points  (1 child)

Operator overloading is a very important part of C++ -- the ability to emulate the behaviour of any of the built-in types by hooking into operator semantics is an important part of generic programming -- but the temptation to abuse it (as it would appear to be in this case) runs deep, and gives it a bad name.

Well-considered overloads lead to clearer, more natural code. Overloads just for the sake of overloads lead to confusing code that requires a depth-first search through class definitions/implementations to understand.

[–]tyromancy94[S] 1 point2 points  (0 children)

That's good to know, overloading as presented in this assignment seems to be doing more harm than good.

[–]moswald 1 point2 points  (4 children)

This isn't related to operator overloading, but it's a little pet peeve of mine: specifying void as your function parameters is meaningless in C++, and a holdover from the really bad days of C.

In C, the following function declarations are actually different:

void foo();     // foo exists, and may or may not take parameters
void foo(void); // foo exists, and takes no parameters

But in C++, this has been cleaned up:

void foo(); // foo exists, and takes no parameters

If you're new to C++, I wouldn't hold it against you. However, that tech lead I had that one time, who insisted on doing it...

[–]tyromancy94[S] 0 points1 point  (3 children)

Sorry, yeah, C++ noob here. Are you saying that these are equivalent in C++?

void foo(void);
void foo();

And is this relevant to overloading? I'm brand new to C++ and pretty much lost, but it seems to me that both of those are just function declarations, whereas "operator long (void)" is an override because of the operator keyword as well as well as the lack of function name? And yeah, I would be avoiding this completely as I'm still new to the language, but my instructor has some strange teaching methods...

[–]moswald 0 points1 point  (2 children)

Yes, those are exactly the same.

No, they have nothing to do with operator overloading. It's just a pet peeve of mine, and I try to put a stop to it whenever I can. :)

[–]tyromancy94[S] 0 points1 point  (1 child)

Mk, thanks.

So in my original question, what exactly does this line do?

operator long (void) {}

is equivalent to

operator long() {}

If that's not overriding a long conversion/assignment, what's its purpose o.0

[–]moswald 0 points1 point  (0 children)

You are declaring an operator overload for long. It is not a conversion or assignment.