Lecture overview -- Keyboard shortcut: 'u'  Previous page: Classes, structs and namespaces -- Keyboard shortcut: 'p'  Next page: Constructors -- Keyboard shortcut: 'n'  Lecture notes - all slides and notes together  slide -- Keyboard shortcut: 't'  Help page about these notes  Alphabetic index  Course home  Lecture 3 - Page 4 : 36
Notes about C++
Abstraction Mechanisms, Part 1
Functions outside classes

In contrast to similar object-oriented languages, C++ allows functions and variables outside classes

Helper functions are typically wrapped in a namespace together with one or more classes.

In the simple case they are placed in the global name space together with the classes

  • The C++ Programming Language: Page 240
 

  • Effective C++, Third edition: Item 23
 

  • Functions outside classes:

    • Which function should be defined outside classes - possible answers:

      • Functions that do not need to access the encapsulated state

      • Functions that we want to use without the dot operator

        • f(obj, ...) instead of obj.f(...)

        • Or instead of Class::f(...) where f is a static member

      • Other functions, such as the entry function main

  • Variables outside classes:

    • Global variables and variables in namespaces

Seen from the C perspective: funtions and variables outside classes represent backward compatibility

Compared with Java and C#: Less emphasis on static variables and methods