Skip to main content

Posts

Showing posts from November, 2013

QT - Signal-slot mechanism

This is first article describing some aspects of programming with Qt framework. I am going to focus on most important basic feature of Qt framework: signal-slot mechanism . At the beginning take a look on below QT code: Output of that code is this window: This quick code is something like "Hello world" of Qt programming. Let's take a detailed look on the code. In point I we are declaring QApplication object which will contain indefinite loop which will be invoked after program starts in order to wait for user interactions. We can also analyse command line input parameters in this line, but we are not doing it because we do not have any input parameters for our application. In point II we are declaring QPushButton object which will be the only object of our application. As only parameter of our definition we are putting text which should be displayed on our button - "Hello World" . Point III is key point of this example. It presents basic

STL - Map keys difference

Today, I would like to show how to quickly find difference between two maps of STL library basing on keys only. We need to compare two maps of STL library and find third map which will contain only those elements of first map which keys are not being placed within second map. Take a look on code: Output of this code is: In point I we are defining struct type having overloaded of operator() . This structure will be used as function object to compare two elements of maps ( std::pair ) basing on keys only. In point II we are defining two maps which will be compared. We are going to receive all std::pair s from firstMap which keys are not being placed within  secondMap . In point II we are declaring diffMap map which will contain result of our differentiation. Point IV is key point of our function. In that point we are using std::set_difference algorithm. This algorithm returns difference of two ranges of STL containers. First 4 arguments are begins and ends of two ranges o

lvalue - definition

Today I found very interesting definition of lvalue  in C++. According to this site: An lvalue is an expression that refers to a memory location and allows us to take the address of that memory location via the  &  operator. An rvalue is an expression that is not an lvalue.  Examples are: // lvalues: // int i = 42; i = 43; // ok, i is an lvalue int* p = &i; // ok, i is an lvalue int& foo(); foo() = 42; // ok, foo() is an lvalue int* p1 = &foo(); // ok, foo() is an lvalue // rvalues: // int foobar(); int j = 0; j = foobar(); // ok, foobar() is an rvalue int* p2 = &foobar(); // error, cannot take the address of an rvalue j = 42; // ok, 42 is an rvalue

Boost - Scoped pointer

Here is the first article about Boost libraries features. I am starting that new series of articles with explanation of scoped_ptr - one of the Boost smart pointers . scoped_ptr is smart pointer that is the only owner of dynamically allocated object and is automatically freed allocated memory when destructor of scoped_ptr is called. See example below: Output of this example is: In point I and II we are defining two scoped_ptr pointers - one for int and one for Test (custom class) object. Notice comment marked as Ia. That is impossible operation for scoped_ptr . It is trying to assign ownership of allocated object to second pointer. scoped_ptr does not allow it. It have to be sole owner of its object. Point III shows how we can assign new values to object which refers to scoped_ptr . We can use dereference operator ( operator* ) as for normal pointers (this operator is overloaded in Boost smart pointers) as well as use get() function implemented to Boost smart po

Advanced C++ - Template classes

Template class concept is similar to template functions . It allows creating generic type classes, which means that we can have many different classes which differ only in terms of types using one class code only. See example below for better explanation: Output of this example is: Point I depicts definition of template class. As you can see it is very similar to definition of template function. We are defining myType as type which will be replaced with real type during usage of template class. Then we are defining class normally with usage of mapped temporary type. Points II and III shows how to use template class for different class. We are defining two instances of our template class - for int and double types. Compiler will implicitely generate bodies of two clases based on those types and template definition. As for template functions, we are selecting real type using angle brackets. Above example you can find on our github repository here: https://github.com/xmeme