Skip to main content

Posts

Showing posts from September, 2013

Advanced C++ - Conversion constructor and operator

When we are defining class we sometimes would like to allow easily convert that class from and to previously defined-type objects (ex. int ). We can do that using conversion constructor and conversion operator: conversion constructor - it is constructor which has one parameter - type which constructor should allow to convert from. It allows to convert previously defined object of type as in constructor paramter into object of class where constructor belongs to conversion operator - it is overloaded operator type() - where type is type of object which we can convert this class object into Let's see below example for better understanding: Output of this example is: In that example in our class TestClass we are defining two conversion constructors. conversion constructor for type int (point I) conversion constructor for type string (point II) Next in main function we are invoking that constructors converting int and string types to our class TestClass type (p

Advanced C++ - Compiler generated functions

When we define some class in our C++ code, compiler is generating some important functions for our class (unless we define it explicitly). Those functions (sometimes called Compiled Generated Functions ) are:  default constructor   destructor   copy constructor   copy assignment operator  During compilation, compiler knows our code and classes usage, so when we are using one of above functions implicitly, it generate that function implicitly for us in the class body. For better explenation, let's see the example below: In point (I) we are defining class TestClass . This class seems to be empty. However we are implicitly using following function in main class:  point III - we are implicitly using defualt constructor in order to create instance of TestClass - default constructor of TestClass is implicitly generated by compiler   point IV - we are using copy constructor of TestClass in order to copy instance to instance2 - copy constructor of TestClass is im

Github Gist code snippets

From today I will use GitHub Gists mechanism to insert code snippets to this blog. Example GitHub Gist for C++ Hello World snippet you can see below. I hope this change make blog readability better. In the free time, I will also try to update previous' posts code snippets using GitHub Gist. UPDATE: Previous' posts code snippets updated to GitHub Gists. Enjoy!

C++11 - Right angle bracket

In C++03 " >> " is always interpreted as right shift operator . It causes problem for nested template declaration like here: In above example for C++03 we have compilation error because of unintended interpretation of " >> ". Compiler interpret it as right shift operator - not like two nested vectors. Proper syntax in above situation should include space character between two " > " characters closing nested templates like here: It it not very comfortable and sometimes causes problems with finding reason of compilation error. Fortunately, C++11 standard solves that problem. It implements proper interpretation of nested templates without space between two right angle operators " > ". Therfore in C++11 following syntax: is properly interprated as two nested vectors.

Advanced C++ - Function object

Function object is object which has function-call operator ( operator() ) defined or overloaded. Thanks to that objects have behaviour of function which means that you can call such object by using paranthenses and passing arguments. Let's see and example: The output of this example is: In point (II) we are defining function object according to class defined in point (I). In point (III) we are using our function object like a normal function.  You may be considering what is advantage of usage function object instead of normal function. Here are few advantages of function object:   1. Function object can be used as normal function but can has also additional abilities related to being object ex. the same function, represente by different function object, can has different states at the same time.   2. Function object has own types. What is more different function objects can has different states even if they signature is the same. This is very useful in STL library. Than

STL - sort() and is_sorted() algorithms

Today I would like to show first article about some example of usage STL ( Standard Template Library ) library and its algorithms. First thing which I would like to present is usage of std::sort() and std::is_sorted() algorithms. Function std::sort() sorts selected range of elements of container in ascending order. Function std::is_sorted() checks whether selected range of elements of container is sorted in ascending order. Take a look at following example of usage those functions: Output of below example is: At the beginning of that example we declare vector of int-typed elements using initializer_list  and prints it using range-based for loop (URL). In point (I) we are using std::is_sorted () function to check whether vector is sorted according to ascending order. Notice that we are iterating through whole vector (from testVector.begin() to testVector.end() ). Function returns false because our vector is not sorted yet. We can see results in first line of output. In

C++11 - Explicit override and final

Consider following inheritance situation: Question is: will we have two override_func() functions in derived1 or only one because override_func(double a) from derived1 overrides override_func(int a) from base_class? Two, of course. It is because those functions has different signature (different types of arguments in this case). However sometimes user does not notice it and think that function override_func(double a) overrides function override_func(double a) in derived1 class. It can cause problem especially if someone is doing some modifications in override_func(int a) in base class and expects that those changes will have inpact on override_func(double a) from derived1 class. C++11 has smart mechanism which allow to notify compiler and other users that in derived class we would like to override function having exact the same signature in base class. It can be done using override keyword. Let's declare override_func(double a) in derived1 class this way: What happend's her

Advanced C++ - Anonymous namespaces

Anonymous namespace is kind of namespace with no specified name. Such kind of namespace is visible only within file where it is defined within. I allows define variables and functions which could be visible as global, but within one file only. Thanks to that such files are not global within whole program. It also allow avoids making global static variable. Let's see an example of defining and usage of anonymous namespace. Consider following main.cpp file: In the above example we have anonymous namespace containing one int typed variable. This variable can be used as global variable withing this main.cpp file but will not be visible inside any other files. That is simple example of usage anonymous variables. If you would like to compile it and play with it (changing or adding some parts of code to check what happends than) you can download it from here:  https://github.com/xmementoit/CppAdventureExamples/tree/master/advancedCpp/anonymousNamespace

C++11 - Tuple type

In C++11 tuple type is collection of heterogeneous (different types) elements of pre-arranged dimensions. tuple is similar to fixed sized vector but the difference is that it can store elements of different types (while vector can store different of only one type). Let’s see some simple definition of tuple type: We defined tuple class having fix size. Size of this tuple is sizeof(string) + sizeof(double) + sizeof(int) . Variable of our test_tuple type will be able to store three variables of different types ( string, double, int ) and can be initialized like here: In order to get value of concrete position of tuple we can use std::get template in the following way: This instruction will get value of 2nd parametr of myTupleObject variable of tuple type (remember - numering from 0 as usual in C++) and assign that value to test_int variable. In order to set (overwrite) value of concrete position of tuple object need ot use std::get template too: You can to do it this w