Skip to main content

Weekly log #3 – Accelerated C++ finished!

This post is actually from the previous 2 weeks.

I finished the book Accelerated C++, by Andrew Koenig and Barabara E. Moo, and started the book Game Engine Architecture by Jason Gregory. Thus, this post is the last highlights through the Accelerate C++.


Assignment conversion

When using the assignment operator (=), the compiler may use constructors with single arguments to implicit convert the right value of the expression. For instance:

The assignment creates a temporary object string that receives “this is a char*” as parameter and copy all the temporary object attributes to the s object.

Friend functions

Inside a class, you can define external functions that are friends, for example:

This means that the function “externalFunction” can access the private properties of MyClass.

Class protection

  • Public methods can be accessed by all objects.
  • Protected methods can only be accessed by children of the class.
  • Private methods can only be accessed by the class that defines it.

Notice that this is different from Java, in which protected methods means that only objects in the same package can access that information.

Inheritance visibility

Inheritance in c++ also has visibility access:

  • In public inheritance, everything is aware of the inheritance.
  • In protected, the child and subsequent children are aware of the inheritance.
  • In private, only the immediate child is aware of the inheritance.


Virtual functions

Suppose you have two classes:

and the function:

Normally, we would expect to be print “1” and “2”, but in both cases the result is “1”. That is because you must explicit say to c++ when you want to use the children methods in polymorphism. You do that with virtual functions:

With this, now we have “1” and “2”.

Note: the polymorphism is only possible when working with references or pointers.

Virtual destructors

This is really, really ugly.

The same example about virtual methods above is also valid for destructors. If you call the destructor of d, without virtual, the Child destructor won’t be called and this will probably send your program to hell. Always declare destructor as virtual if you want to use inheritance.

Pure virtual methods

When you do:

You are declaring m as a pure virtual. This means:

  • The method m is abstract and MUST be overridden by children.
  • The class is abstract and CAN’T be instantiated.

Forward declarations

Used when you need to use a name before defining them. For example:

Storage-class specifiers

Determine location and lifetime of a variable:

  • register: suggests that the compiler should try to put the variable into a register (in order to optimize the access);
  • static: preserves a variable after leaving the scope;
  • extern: tells the compiler that this is not a definition, only a declaration. The variable is defined somewhere else;
  • mutable: allows a variable to be changed even when an object is const.

How the book looks like after all annotations

Leave a Reply

Your email address will not be published. Required fields are marked *