Quasar
Moderator

Posts: 5139
Registered: 08-00 |
RjY said:
Quasar: congratulations on your successful language transition. As I said I'm not entirely convinced it wasn't an overreaction, but I seem to recall you said you wanted to do some stuff with C++ features anyway, so I guess it's all good.
I'm interested to know what SoM thinks of all this as I don't think I've seen him express an opinion on choice of language.
Personally I need to find some kind of C++ for C programmers guide because I have only the vaguest idea of what C++ gives you over C...
The problem wasn't that C99 limitations couldn't be worked around in the short term, but that working around them in the long term while continuing to develop designs that ran counter to them would probably not be sustainable. And besides that, it was clouding what would otherwise have been elegant implementations with low-level pointer juggling and type casts.
C++ offers two main things: classes with polymorphic inheritance, and templates for generic programming.
Classes are just an extension of C's structs, which add the ability to have access control (public/protected/private), inheritance, encapsulation of methods (functions) with the data on which they act (along with a "this" pointer to use inside them that implicitly references the current object), constructors and destructors for (de)initialization, and most importantly, the "virtual" function call mechanism - meaning the method called is selected based on the most-derived type of an object, at runtime, and not the type of the pointer or reference it was called through.
Templates allow a piece of code (a class or function) to be defined in a type-agnostic manner. For example:
code:
template<typename T> T addTwo(T &a, T &b) { return a + b; }
You can call that template function on any two objects which implement an operator + that returns T or T &. And that includes fundamental types like int or double. (unary & represents a reference, btw, which is just an opaque pointer which isn't allowed to ever be NULL).
|