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.

class Example {
    Example( int = 0, int = 0 );
    int dataA;
    const int dataB;


class Example {
    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 {
  virtual void draw() const = 0;  // = 0 means it is "pure virtual"

class Circle: public Shape {
  void draw() const {}


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.



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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s