full disclosure – this is for a homework assignment. And I normally would not ask for homework help, but here it is.
I’m asked to provide 5 examples of “overloading implicit in c++”. I’m sure he is referring to operator overloading for types such as char, int, float, etc in iostream and the types themselves.
I understand explicitly overloading an operators such as (my dumb example)
class Vegetables {
public:
Vegetables();
~Vegetables();
Vegetables& operator+ (Vegetables&);
Vegetables& operator- (Vegetables&);
private:
int beans;
... // more veggies here
};
Vegetables& Vegetables::operator+ (Veggies&) {
beans += Veggies.beans;
...
return *this;
}
So I am just trying to decide if he is referring to the overloading that is “implicit” when adding types. For example, int+double. I think what actually occurs is int gets cast as a double, then the double + operator is used and a double is returned? Of course, the way this happens varies based on if it is a value assignment or in iostream or other i/o method, etc. But my point remains…
.
It’s late. I’m tired. But this is intriguing…
“overloading implicit in c++” is far from clearcut.
This smacks of someone creating their own terminology. Perhaps they are merely unable to view their own writings from a third-party perspective and are therefore being obtuse. More than likely, your teacher is busy with his own research projects (or other work), and is doing the absolute minimum effort required to teach you. It sucks. You pay a lot of money and you’re treated horribly. I’ve been there myself. All I can say is, get used to it. Or find a better school. And next time, read the homework ASAP, and ask your teacher! If enough folks bother him, wasting his time, he’ll get his act together.
Next thought: Try google. Always your friend.
My thoughts: He may be talking about overloaded functions that are automatically (implicitly) created, such as the copy constructor. Consider:
With the copy constructor commented out, you will find only f(2) and g(3) invoke Foo(int). The Foo h=f line is correctly initializing h through an implicit copy constructor that overloads Foo(int). (Caveat Emptor: I’m still stuck using the outdated gcc/g++ 3.4.4 & 4.0.1. Your mileage with your compiler may vary…)
You might also get some mileage out of subclassing, as copy constructors and operator=() are not inherited, and new implicit versions are created.
Another thought: As (everyone) has already mentioned, your instructor may be thinking of how types are converted. For instance, we could initialize g(3.2) using a floating point value. It would be converted to an int, and work with Foo(int). (Depending on your compiler settings, you might [should!] get a warning about loss of precision.)
We can go the other way too. Adding a line to class Foo like:
Means we can now write:
Wherein h is automatically converted to an int.
Automatic conversions can transpire between quite a number of built in types. Between double/long-double/float and int/short/long/etc and signed/unsigned and etc. (Incidentally, that’s a really good way to screw yourself up, by invoking the wrong overloaded function by mistake. Converting -1 to unsigned makes for big numbers… Accidentally doing unsigned math where you mean to do signed math is another easy gotcha!)
Pointers can be quietly converted to (void*). Consider:
Of course, the obvious case that everyone harps on is polymorphism, wherein we pass a derived class, and it’s received as a base class.
There’s another case wherein we pass data, and a new temporary object is created from that data for the invoked function/method.
There are variable-argument lists, that whole ellipsis thing (printf(format,…)). (E.g. #include <stdarg.h>. va_start(), va_arg(), va_end().) Implicitly overloaded, but probably not what he was thinking about…
There are macros, both regular #define FOO(X) and variable-argument-list macros… Again, clearly overloaded. Again, probably not what he was thinking of…
There’s templating, though that would seem to fall into the realm of explicit overloading…
Bottom line: Only your teacher knows what the hell he wants… Good luck!