Skip to main content

Posts

Showing posts with the label boost

Boost.DateTime - Selected date operations

Today I would like to present you operations related to date and time using Boost.DateTime library. This library contains set of classes, templates and algorithms prepared for date and time related tasks. This article focuses on date operations . Time operations will be presented in one of the future articles. Example of usage some basic Boost.DateTime date operations you can see here: Output of this example is (for day 2 January 2014): All below operations will be based on boost::gregorian namespace. We created alias dateTime for that namespace for more convenient use. In point I we are using universal_day() method for getting current day according to UTC time zone. Of course output of this and other date-related functions depends on date of invoking. Therefore you will get other output of your application every day you will invoke it. universal_day() function returns Boost   date class object which has lot of functions for date manipulation and displaying. ...

Boost - shared pointer

Shared pointer is another kind of Boost library's pointer. It is similar to scoped pointer but shared pointer allows share ownership of object. We are able to assign two or more pointers pointing to the same object (it was impossible for scoped pointer). Take a look to the example below: Output of this example is: In point I we are initializing two shared pointers pointing to two separate instances of TestClass class, then we are printing its values in point II. Point III shows use difference between shared pointer and scoped pointer. We are assigning two pointers to the same object of class TestClass . In this point object having testValue==5 is being destroyed (because we remove the only one pointer to that object). Also, we have two pointers pointing to objects having testValue==6 now. Note that that object will be destroyed when last shared pointer pointing to them will be destroyed ( int our case it is destroyed when we are reaching end of main() function)...

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...