background preloader

Tutorials and Tips

Facebook Twitter

Encoding and decoding base64 with C++ Converting wstring bytes to string... How to convert string to wstring? [Archive] String to char* This article is intended for programmers who are starting C++ programming and have a background of C knowledge.

string to char*

Yes, it is true that C++ inherits most of C, but there are many things that should be avoided, such as preferring new and delete to malloc and free, the C++ casts to the C ones or the use of STL containers to statically or dynamically allocated arrays. I will bring into the discussion a special case of the later: preferring std::string to char*. I will take you through a list of problems with the C-like character arrays and then show how much you benefit by avoiding it and using std::string. Avoid Ill-Formed Declarations of char Arrays When declaring a char array, many programmers do it like this: char* name = "marius"; That might look okay at first glance, but then you might decide you need to make the strings begin with a capital. Dr Dobbs - Generic<Programming>: A Policy-Based basic_string Implementation.

URI Encoding and Decoding. Inter-Thread and Inter-Process communication. Inter-Thread Communication You are best advised to handle inter-thread communication by means of the wxWidgets event handling system, more precisely, by posting events to the message handler of the parent.

Inter-Thread and Inter-Process communication

Usually, there is the classical main-thread-worker-thread scenario. Sending events to the main thread - wxWidgets 3 only For wxWidgets 3, it is possible, in addition to the below methods to make use of CallAfter as a potentially 'cleaner' way of doing this will less boiler plate code. e.g. thread.cpp Sending events to the main thread For simple cases, there is no need to create a custom event, you can simply use an existing event type with a specific ID. thread.h main.cpp. Copy a streambuf's contents to a string.

C++, boost asio, receive null terminated string. GetInputStream() in wx280 [wxWindows] WxSocket in a secondary thread. Type Casting. Implicit conversion Implicit conversions are automatically performed when a value is copied to a compatible type.

Type Casting

For example: Here, the value of a is promoted from short to int without the need of any explicit operator. This is known as a standard conversion. Standard conversions affect fundamental data types, and allow the conversions between numerical types (short to int, int to float, double to int...), to or from bool, and some pointer conversions. Converting to int from some smaller integer type, or to double from float is known as promotion, and is guaranteed to produce the exact same value in the destination type.

For non-fundamental types, arrays and functions implicitly convert to pointers, and pointers in general allow the following conversions: Implicit conversions with classes The type-cast operator uses a particular syntax: it uses the operator keyword followed by the destination type and an empty set of parentheses. Keyword explicit This may or may not be what was intended. URI Encoding and Decoding. Arrays. Problems with Boost.Thread.hpp - C++ More explanation of CoInitialize() and CoUninitialize() In this article, I'll not talk about how it works about the COM's apartment model, but the coding practice.

More explanation of CoInitialize() and CoUninitialize()

CoInitializeEx() provides the same functionality as CoInitialize() except it provides a parameter to specify the thread's apartment model. So, MSDN recommands us to call CoInitializeEx() instead of CoInitialize(). For using CoInitializeEx(), we also need to follow the same rules as CoInitialize(), I mentioned in the previous article. The following examples explain how to use CoInitializeEx() and CoUninitialize(): Example #1: CoInitializeEx() will return S_FALSE if the COM library was loaded. void main() HRESULT hr = E_FAIL; // The first call hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); // hr = S_OK // The second call hr = CoInitialize(NULL); // hr = S_FALSE, CoInitialize(NULL) = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED) // ... skip ... // For the second call CoUninitialize(); // For the first call CoUninitialize(); // COM library will be unloaded.

ATL String: What's wrong with the USES_CONVERSION macros? How to avoid using them? Creating objects on the heap using new : instance object « Class « C++ Tutorial. Class instance inside class? C++ C++ Reference Guide > The std. Last updated Jan 1, 2003.

C++ Reference Guide > The std

If you’re looking for more up-to-date information on this topic, please visit our C/C++ Programming article, podcast, and store pages. Linked lists are another widely-used data structure. Yet writing a linked list from scratch is an arduous, frustrating and time-consuming task. Instead of reinventing the wheel every time anew, use the STL std::list container class.

Its uniform interface, generic nature and seamless integration with other STL constructs make it a superior choice to any homemade list class, as I will show in the following sections. Creating a list object The header <list> contains the declaration of std::list and its specialized algorithms. [C++] How to Create Instance of another class? - Neowin Forums.