logo

C++ - Functions

non-member functions vs member functions

  • non-member functions, a.k.a. free functions: do not belong to any class.
  • member functions, a.k.a. methods(but "method" is not used in the C++ standard): part of a class or a struct.

Types of functions

  • overloaded functions
  • static functions
  • inline functions
  • operators
  • const member functions
  • virtual member functions
    • pure virtual functions
  • special class functions: default constructor, copy constructor, copy assignment operator, destructor; (since C++11) move constructor, move assignment operator
  • function templates
  • variadic function templates (C++11)
  • constexpr functions (C++11)
  • consteval functions / immediate functions (C++20)
  • override and final for virtual functions (C++11)
  • = default and = delete functions (C++11)
  • lambdas (C++11)
  • coroutines (C++20)

inline functions

The code of the inline function is inserted or substituted at the point of its call during the compilation instead of using normal function call mechanism.

inline is only a request to the compiler. The compiler may or may not perform inlining.

virtual

A virtual function in C++ is a member function declared within a base class that can be redefined (overridden) in a derived class.

When a virtual function is called through a base class pointer or reference, the actual function executed is determined at runtime based on the object's actual type. This mechanism is known as dynamic binding or runtime polymorphism.

Pure virtual function (= 0): subclasses have to implement this function, otherwise they are abstract. E.g. virtual std::string ToString( ) = 0;

Lambda

Use lambda expressions as anonymous functors.