std::move explained with simple example

std::move is a C++11(Introduction to C++11) utility function which is used to convert an lvalue to rvalue expression to forcefully call the move constructor or move assignment operator which will improve the program’s performance drastically. This utility function is only change lvalue into rvalue expression that’s it. This function doesn’t do any kind of memory movement.

Implementation of this function is as follows:

From C++11 onwards:

template< class T >
typename std::remove_reference<T>::type&& move( T&& t );

From C++14 onwards:

template< class T >
constexpr typename std::remove_reference<T>::type&& move( T&& t );

Let’s take an example of usage of this move function:

(more…)
std::move explained with simple example Read More

Move Constructors explained with simple example


In C++11 (Introduction to C++11) , move constructors are added which can improve any code’s performance drastically. Typically a move constructor is same as copy constructor which will create a new instance based on the passed object. But, the idea behind move constructor is to avoid memory reallocation and use as many memory from the passed original object because the original object is about to be deleted as it has been provided as a temporary object.

Since, in move constructors we avoid memory reallocation while creating new instance while will improve the code’s performance as we know memory allocation is quite costly.
While implementing move constructors we need to take care of one important point to ensure that the original object can be correctly destroyed.

Let’s take an example to see how this memory reallocation is avoided in case of move constructor.

(more…)

Move Constructors explained with simple example Read More

Rvalue References


Before understanding about this new feature “Rvalue references” of C++11(Introduction to C++11), we should first understand what is lvalue and rvalue in C++.

Typically Lvalue is an expression which returns a permanent memory address which can be assigned to any variables. For ex:

int x = 10;
int y;
y = 20;

Here both x and y are lvalues. In some cases, we can have functions also as lvalue as shown in below example:

int x;  // global variable

// Returning reference to global variable x
int& getXRef()
{
    return x;
}

getXRef() = 30;

Now this function can act as lvalue too as shown above.

(more…)

Rvalue References Read More

Introduction to C++11

In August 2011, a new C++ version which is known as C++11 is approved by ISO which adds a lot of new set of features to existing C++ programming language. All official documents related to these changes can be found at ISO C++ committee website.

Why C++11 ?

C++11 significantly improved the core C++ language by making several additions to its standard libraries. Major areas where C++11 is improved from its predecessor includes multithreading support, uniform initialization, generic programming support and performance.

Aims of C++11

  1.  Compatibility and Stability MUST be maintained for old code written in C++98,C++03 and in C.
  2.  Extension of core language preferably be done via standard library.
  3.  System and library design method is preferred compared to introducing new features which is useful only for specific applications.
  4.  Focus to improve type safety by providing safer methods to earlier unsafe methods.
  5.  Improve performance and better interaction with hardware to ensure better,safe and high performance embedded system programming.
  6.  Make C++ easier to teach and learn through increased uniformity, stronger guarantees, and facilities better libraries for beginners. Expert programmers can use other efficient features.
(more…)
Introduction to C++11 Read More

Command Design Pattern Explained With Simple Example: Behavioural Design Pattern Category

The Command design pattern allows to encapsulate an action or trigger inside an object which will be used later to trigger an event. Since in this design pattern, commands are encapsulated inside objects, hence we can use additional actions on this commands for example- Queuing of various commands, undo/redo actions etc. This design pattern is very useful in case of GUI actions (button, menu actions etc), transactional behaviour, progress bars etc. For Design patterns basic explanation see (Design Patterns Simplified Version).

(more…)

Command Design Pattern Explained With Simple Example: Behavioural Design Pattern Category Read More

Chain-of-Responsibility Design Pattern Explained With Simple Example: Behavioural Design Pattern Category

The Chain-of-Responsibility design pattern basically consists of a source of command objects and a hierarchical series of processing objects. Every processing unit will process only typical kind of commands and it will pass rest of the command to next processing unit, thus creating a chain of processing units and that’s why the name of this design pattern is Chain-of-Responsibility pattern. This pattern provides the idea of one of the best programming practice which is “loose coupling”. For Design patterns basic explanation see (Design Patterns Simplified Version).

(more…)

Chain-of-Responsibility Design Pattern Explained With Simple Example: Behavioural Design Pattern Category Read More

Proxy Design Pattern Explained With Simple Example: Structural Design Pattern Category

The Proxy design pattern allows to define a “proxy” class which will act as a wrapper object which will be called by the client to access the product object without exposing the details. This proxy class can also be used to add extra functionality without changing the product behavior. This design pattern can be used in cases to provide additional security access to an existing product or providing an interface for remote resources. For Design patterns basic explanation see (Design Patterns Simplified Version).

(more…)

Proxy Design Pattern Explained With Simple Example: Structural Design Pattern Category Read More

Flyweight Design Pattern Explained With Simple Example: Structural Design Pattern Category

The flyweight design pattern allows to greatly reduce memory footprint of any product by dividing an object into basically two parts. If multiple objects have some internal part as common then all these objects can share these memory to reduce memory consumption. The other part which varies from object to object will still be part of final object. The common part of various object is stored and shared via a “Flyweight” object. For Design patterns basic explanation see (Design Patterns Simplified Version).

(more…)

Flyweight Design Pattern Explained With Simple Example: Structural Design Pattern Category Read More

Facade Design Pattern Explained With Simple Example: Structural Design Pattern Category

The Facade design pattern provides a simple interface to a complex code in order to make the product easier to understand and use. This design pattern reduces the dependency of client code on the implementation of internal objects and since most of the product code uses the facade to hide the complexity of the system, thus allows the flexibility in developing the system. In this pattern, a single wrapper class known as “Facade” is implemented which contains a set of objects required by client. For Design patterns basic explanation see (Design Patterns Simplified Version).

(more…)

Facade Design Pattern Explained With Simple Example: Structural Design Pattern Category Read More

Decorator Design Pattern Explained With Simple Example: Structural Design Pattern Category

The Decorator design pattern allows behavior addition to an object either statically or dynamically without affecting the core behavior of other objects from the same class. This design pattern allows us to change the behavior of any specific object which is achieved by a wrapper class to the original class known as Decorator class. The original class is divided into two parts, one is with the essential basic features of the original class while the other one is the decorator base class which implements the same interface as the original class but allows to create several concrete decorator class having their own additional methods. For Design patterns basic explanation see (Design Patterns Simplified Version)

(more…)

Decorator Design Pattern Explained With Simple Example: Structural Design Pattern Category Read More