next up previous contents index
Next: iostream.h statt stdio.h Up: 50 Ways to Improve Previous: 50 Ways to Improve

const und inline statt tex2html_wrap_inline5149 define

   Den Compiler dem Präprozessor vorziehen:

    const double RATIO = 1.472;

statt

    #define RATIO 1.472

Vorteil:

Definitionen wie

    #define MAX(a,b) ((a)>(b)?(a):(b))

müssen immer korrekt geklammert sein. Selbst dann kann es Ärger geben:

    int a = 1;
    int b = 0;
    MAX(a++,b);   // a wird 2mal erhoeht
    MAX(a++,b+2); // a wird 1mal erhoeht
    MAX(a,"hi");  // Typen!!!

Die einfache und sichere Lösung ist:

    inline int MAX(int a, int b)
    { return a > b ? a : b;
    }

Der Präprozessor ist natürlich noch für bedingte Übersetzung wichtig (#ifdef, #ifndef, usw.).  


next up previous contents index
Next: iostream.h statt stdio.h Up: 50 Ways to Improve Previous: 50 Ways to Improve

Peter Pfahler, 1997