Thursday, 3 December 2015

Low-Level Programming_Post11

Low-Level Programming: Post 11 03/12/2015

December 3rd 2015 is the date of the eleventh lesson on Low-Level Programming.

In a case similar to last week, we started the lesson showing, from the bullet points, what we were going to learn in the lesson. The reason as to why I stated that this lesson is very similar to last week's lesson is that, although there were some bullet points, there wasn't a lot that we were going to be taught today.

Below are the bullet points and what we covered in the lesson today...

  • Operator Overloading: What is Operator Overloading and how can it benefit us?
  • Function Pointers: How do we utilize function pointers?
  • Delegate Functions: What's a delegate function and how do we use them?
Overloading
  • Functions: It's possible to overload functions, we do this by altering the parameters on the function signature.
  • Operators: All objects and classes have a number of operators. These range from constructors through to less than or equality comparisons.
  • Overloading: The compiler where possible will generate simple operator functions. However, these can be overridden or overloaded.
  • Syntax: Overloaded operators are functions and like a normal function, they have return types and parameters.
  • Copying: One example of using operator overloading is when a class allocates memory on the heap.
  • Assignment: When the object is assigned, there's a need to ensure we do need a deep copy of all those heap values and not point to the same ones.
  • Casting: By overloading the construction operators, we can enable implicit and explicit conversions between differing types.
  • Conversions: Before making conversions between different types, it's important to make sure the conversion is logical as to avoid confusion.
  • Equality: One of the most common usages of operator overloading is the equality operation ==. We overload the room equality to do a string comparison on it's description.
  • < Operator: Remember that the STL uses the less than operator for maintaining it's sort order when using associative containers.
  • Operator += & -=: Depending on an object's definition and structure, it may be valid for it's value to increase or decrease.
  • Restrictions: The operators :: (scope resolution), . (member access), .* (member access through pointer to member), and ?: (ternary conditional) cannot be overloaded.
  • Non-Existent Operators: New operators such as **, <>, or &| cannot be created.
  • Common Functionality: The overload of operator -> must either return a raw pointer or return an object (by reference or by value), for which operator -> is in turn overloaded.
  • Precedence: It is not possible to change the precedence, grouping, or number of operands or operators.
  • Short Circuiting: The overloads of operators &&, ||, and , lose their special properties: short-circuit evaluation and sequencing.
Function Pointers
  • Functions: The function identifier is a constant pointer to a function. The pointer holds the address in memory where the code for the function resides.
  • Dereference(): When the function is called via the () operator, execution jumps to the address of the function via it's constant pointer.
  • Syntax: Knowing this, it becomes possible to store the address of a function in a variable, but it's a pretty hideous syntax.
  • Parentheses: They're important due to precedence rules in the language. In this case it would be seen as a function that returns a pointer to an integer.
  • Ptrs to functions: Storing pointers to functions mean we have to dereference the pointer. There are two ways of doing this: Implicit calls and Explicit calls.
  • Callback Functions: Passing a function pointer through to another function is possible. It just needs to be added as an argument to the function signature.
  • Decltype: Evaluates the type of an expression. We can use this to cheat on our declaration of function pointers as a type.
  • Compiler: The compiler evaluates the expression for you and substitutes it's reference when compiling.
  • Auto Keyword: When storing a Ptr to a function inside the code base, it's preferable to use the auto keyword to prevent the convoluted syntax involved.
  • Decltype of Auto: It's even possible to decltype an auto defined variable. However, this may make it less clear when reviewing the code.
  • Factories: We can make use of function pointers when creating a factory class.
  • Each class has a lookup ID that's stored in the pair. The second part stores the function pointer that's responsible for creation.
  • Pointers: Assuming the signature of all the action handling functions were the same, why not use function pointers instead.
  • Predicates: Predicates are another example of function pointers and are used throughout the STL and it's containers and algorithms.
  • Static Predicates: By implementing a static predicate inside a class, it can be provided to the algorithms.
  • Lambda Functions: Lambda or local functions can be used, again these are passed through as a function pointer to the algorithm being utilized.
  • Classes: It's possible to have a pointer to a member function. In order to use these, we need an object and some new syntax.
  • This Pointer: In essence, in order for member functions to be accessed via pointers, they need access to the pointer.
  • Normal Pointer: A normal pointer can only store one address. This is why it's not possible to have a direct pointer to an object and the function.
Functors
  • Functors: Function objects or functionals are object + (). A functor is an object that can be used with () in the same way a function is called.
  • Functions: This includes normal functions, pointers to functions and class objects where the () operator is overloaded.
    • STL: The STL makes extensive usage of function objects, the most obvious example is when a predicate is required.
    • An Object or a Function: It's an object that acts as a function. One of the best usages of a functor is the storage of state.
    • Struct: Define a struct that we will use as a function.
    • Overload the (): Overload the () operator, this way we can call the class as a function and it will perform the code in the () operator.
    • State: This struct is no state. We could easily add member variables to it, if they would help in the function's processing.