C++

Shooting your foot: You accidently create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying "That's me, over there."

- found on the net in Tallus Humor Archives (I think)

"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off."

- Bjarne Stroustrup, the DANISH designer of C++ (from his FAQ at his homepage)



C++ - an object oriented programming language

The foundation for C++ was created by the danish computer scientist Bjarne Stoustrup in 1983 with "C with classes". The language was developed on top of C to allow easy porting of old programs to C++. The core of C++, kernel C as some call it, is to a high degree the same as C with a bit of nice add-ons like overloading and call-by-reference. During the late 80'ties and early 90'ties, C++ gained acceptance around the world as a fast high-performance language, but it suffered from one thing - there was no final standard, so all compilers had their little tweaks to the core. A standard was not completed until mid 1998, where ISO and ANSI finally agreed to a standard C++.

The late time for a standard has resulted in an absence of compatible compilers - the currently best implementation of C++ is GCS from the GNU group and C++Builder 4 from Inprise. Later on I return to compatibility.

The ANSI/ISO standard

Object model

The object model used in C++ is quite like the first version of Borland Pascal that supported objects - classes are a deriviation of structures with a couple of new functions associated. This means that objects does not need to be references but can be automatic and created on the stack. But again, this means that you must avoid returning a reference to an object not dynamically created - a hard-to-track bug.

Exception Handling

When you get used to it, you don't want to miss exception handling. Exception handling is a very convenient way to handle errors in a program - it allows you to jump serveral levels out of function calls and redirect execution of your program to a very different location if an error occures. This allows you to move your error handling code out of the normal execution of your program and into special error-handling blocks. When you first get hooked on exceptions, you never want to lose them!

Type-casting

The new ISO/ANSI standard disrecommends the old kind of type-casting like (int) float_value and recommends a whole new set of different keywords for type-casting. The new keywords are:

Overloading and default values

C++ supports overloading of functions - you may now have two functions with same name but different signatures. A signature is the list of types for the function called, like:
int booh(int a, float b) - has the signature int*float->int
int blah(float a, double b) - has the signature float*double->int
Now, according to the new standard, blah may be named booh since they have different signatures.
Default values can be added like:
float power(float e, int pow=2) - here the signature is float*(int=2)->float. If only one parameter is supplied, the compiler assumes the other parameter is 2. Default values can only be assigned right-to-left.
Operators may be overloaded as well - this makes the language look pretty neat when working with different classes. If you want to take a look at this, you can go to the software page, where you can download classes for vector and matrixcalculations using operator overloading.

Call-by-reference

Users of Pascal may have missed the var-statement used as a prefix for a variable passed to a function. The var prefix forced a call-by-reference instead of call-by-value. This was not supported directly in C - you had to use pointers. This has been fixed in C++ - you may now use an & when dictating the type for a function parameter to indicate a call-by-reference.

Compatibility

As earlier mentioned, the late ANSIfication has resulted in different problems with compatibility. Here, I have listed the three most (I think) important new features of C++. The reference for the list is DJGPP, meaning that the source I tested with could be compiled and run correctly under DJGPP 2.03 since I consider this the most ISO C++ compatible compiler. Please send any updates to this list:

Name Web Exception handling Templates New typecasting
Borland C++ 2.0 www.borland.com - ? -
Borland C++ 3.1 www.borland.com - V -
Borland free C++ 5.5 www.borland.com V V1 V
Watcom C++ 10.6 www.openwatcom.com V V2 -
DJGPP 2.03 www.delorie.com/djgpp/ V V V
GCS 2.95 gcc.gnu.org/ V V V

1) When working with strings, it gets so confused over its own templates that it somtimes chokes resulting in errors no-one can interpret, therefore I still have to make it compile my R1-emulator.
2) Problems with friends in template-classes.



Back...





Last updated 2001-08-22 FlushedSector