General Hints, Tips and Tricks for C++

Frank B. Brokken / Jurjen Bokma

This page provides hints, tips and tricks which may prove useful when solving the exercises of the C/C++ course. Use them to your advantage. The bracketed names given with the hints refer to the set of exercises from where the hint can/should be applied.

New hints may be added to the various sections while the course is running. In those cases: newly added hints are flagged by a -image

Hints WRT Part I Hints WRT Part II Hints WRT Part III

Hints WRT Part I


Intro

About using comment:



Expressions



Flow



Functions



Libraries

The following describes how to create and use (static) libraries. Dynamic libraries are, construction-wise, a different topic, and may or may not be covered at some point during the course. For the time being, all that needs to be done can be done using (your own) static libraries.

The library is now constructed. Hereafter it can be used:



Classes

  • When defining static data resist the temptation to initialize them in the header. Although that is sometimes possible, it's unclear where those static data are actually defined, and so if you ever have to change their initializations you frequently end up having to recompile all your files. Instead, use a file data.cc in which you define and initialize the static data. That way the header remains as it should: it declares instead of defines the class's elements.

  • The above reasoning does not hold true when initializing data members in class header files, because those initializations are used by default in constructors that do not explicitly initialize those members. In constructors it's always possible to overrule the default by explicitly initializing those header-initialized data members.



  • Pointers

    pointers2:



    BasicIO



    ClassAlloc


    [an error occurred while processing this directive]