Initializing const field:
If you have a const data field that needs to be initialzed, it has to be done before the curly braces of the initialization function. Always wondered if there was a strict case of why initialization had to be done there.
example.h class Example { public: Example( int = 0, int = 0 ); private: int dataA; const int dataB; }
example.cpp class Example { public: Example(int a, int b) : dataB( b ) { dataA = a; } }
Virtual Function:
If a virtual function has =0 to it’s end it is a pure virtual function. That means that child classes that derive from the the base class must implement the virtual functions.
class Shape { public: virtual void draw() const = 0; // = 0 means it is "pure virtual" ... }; class Circle: public Shape { public: void draw() const {} };
Source:
http://www.parashift.com/c++-faq-lite/pure-virtual-fns.html
Smart Pointer:
A smart pointer is a class object that acts like a pointer but has additional features. The main additional feature is that a smart pointer will automatically free allocated memory. This is implemented as smart pointers are objects and can delete and free in the destructor.
the std library supports:
auto_ptr – only one smart pointer allowed to own a particular object.
unique_ptr – more restrictive version of auto_ptr. Should be using this.
share_ptr – uses reference counting to keep track of how many smart pointers refer to a particular object and only when the final pointer expires would delete be invoked.
USE shared_pointer, if the program uses more than one pointer to an object.
Else use unique_ptr. unique_ptr good choice for return type for a function that returns a pointer to memory allocated by new. That way ownership is transferred to the unique_ptr assigned the return value, and that smart pointer takes on the responsibility of calling unique.
Reference:
http://stackoverflow.com/questions/417481/pointers-smart-pointers-or-shared-pointers
C++ Primer Plus, Sixth Edition
Strong vs Weak References:
In C++, normal pointers are weak and smart pointers are strong.
Strong reference is a normal reference that protects the referred object from collection by a garbage collector (GC).
Weak reference does not protect the refereced object from collection by a GC