Just to give people some insight on how hard C++ is to completely master, I would like to mention that just yesterday I was hacking some awful C++ overloading stuff, writing Lua bindings for a project. Anyway, in C++ you can overload pretty much everything, but the correct version is always selected by parameters.
I needed more. I needed to overload a function based on it's return type instead. Now, I've been programming C++ over 7 years (at least). I would think I know the language pretty damn well, including common problems different compilers have for stuff like templates. And I certainly did know that you can overload the assignment operator, but you can only overload it inside your class definition, which means you need to be writing the target of the assignment.
What I must have either have missed for seven years (or forgotten at some point), is that you also overload the cast operators. WHAT? Well, when you use an integer in a floating point operation, it is automatically converted. Similarly, you can force that conversion by writing:
Obviously the above would work without the explicit "cast" in this case. The interesting thing though, is that it's possible to write such conversions for classes. And the really interesting thing is that it's done in the "source" class, the class from which we convert:
Code: Select all
class Foo {
operator int() {
return 0;
}
};
The above would make objects of class Foo convertible to integers, in this case with a constant value of 0. Quite esoteric syntax isn't it? Anyway, I thought I
knew C++. Still, I had missed such an useful construct for all that time.
So, don't expect to learn C++ fast. There's a LOT of stuff in there.
As for what Candy says, I think it's not enough to know Standard C++ OR what your compiler accepts. You need to know both. In fact, you also need to know what other compilers accept. The union of all the existing dialect of C++ is what you need, so you can actually hope to write portable code. Oh, and C++ isn't static. Some perfectly valid C++ of mine from 5 years ago doesn't compile with recent compilers anymore, because the standard has changed. Not much, but enough to make recent gcc complain.