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] Prefer std::string to char* This article is intended for programmers who are starting C++ programming and have a background of C knowledge. 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.

Name[0] = 'M'; const char* name = "marius"; Nasty C-Like Approach What you can do is use a char[] to define a fixed-length array of chars: That works. 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. 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 Sending custom events to the main thread The example below fully illustrates how to create a new event type; but keep in mind that for simple needs, it might be enough to use a wxCommandEvent with a specific ID reserved for this task (see above) In the main thread code : In the worker thread code : Bidirectional Communication 1.

Copy a streambuf's contents to a string. C++, boost asio, receive null terminated string. Problem with wxURL::GetInputStream() in wx280 [wxWindows] From: Troels on 19 Dec 2006 11:22 Francesco Montorsi wrote: > You need to get access to the wxProtocol associated with the wxURL and > use SetTimeout with a reasonable value. Or perhaps call SetFlags(wxSOCKET_NOWAIT)? (Bug #1612106) Regards From: Piotr Starczewski on 19 Dec 2006 12:00 Using debugger I have found out that wxURL::m_error = wxURL_NOERR before I'm calling GetInputStream() and it's changing the state to wxURL_PROTOERR after GetInputStream(). Peter chris elliott wrote: > how about ... > wxURL url(myURL); > if (url.GetError() ! > return NULL ; > wxInputStream * pInput = url.GetInputStream() ; > what is the URL? > chris > Piotr Starczewski wrote: >> Hello, >> I have a problem with getting input stream from http site. >> constructed wxURL, but it seams to hang on wxURL::GetInputStream().

>> Does anyone else have such problem? >> it? >> Here is a sample code that I'm using: >> wxURL url(__url); >> if (url.IsOk()) >> wxInputStream* _in_stream; >> _is_stream = url.GetInputStream(); // <-- Hangs here and. WxSocket in a secondary thread. Type Casting. Implicit conversion Implicit conversions are automatically performed when a value is copied to a compatible type. 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. Other conversions between arithmetic types may not always be able to represent the same value exactly: For non-fundamental types, arrays and functions implicitly convert to pointers, and pointers in general allow the following conversions: Implicit conversions with classes Keyword explicit On a function call, C++ allows one implicit conversion to happen for each argument.

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. 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::list Container Class. Last updated Jan 1, 2003. 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.

#include <list> using std::list; int main() { list <int> li; } To insert a single element, use the push_back() member function: li.push_back(1); To remove the last element of a list, use the pop_back() member function li.pop_back(); li.push_front(0); // the first element is now 0, not 1 li.pop_front(); [C++] How to Create Instance of another class? - Neowin Forums.