c++

Open source examples of well designed applications

Do you know of well designed open source applications that are instructive to analyse? Specifically, I'm interested in practical applications object-relational mapping in C++ based programs, where there is a good separation between a domain model and persistence/serialization functionality. ...

Is TCHAR still relevant?

I'm new to Windows programming and after reading the Petzold book I wonder: is it still good practice to use the TCHAR type and the _T() function to declare strings or if I should just use the wchar_t and L"" strings in new code? I will target only Windows 2000 and up and my code will be i18n from the start up. ...

Generating a Deck of Cards

I'm trying to make a simple blackjack program. Sadly, I'm having problems right off the bat with generating a deck of cards. #include <iostream> #include <vector> using namespace std; int main() { vector<char> deck; char suit[] = {'h','d','c','s'}; char card[] = {'2','3','4','5','6','7','8','9','10','J','Q','K','A'}; f...

Do polymorphism or conditionals promote better design?

I recently stumbled across this entry in the google testing blog about guidelines for writing more testable code. I was in agreement with the author until this point: Favor polymorphism over conditionals: If you see a switch statement you should think polymorphisms. If you see the same if condition repeated in many places in your cl...

using STL to find all elements in a vector

I have a collection of elements that I need to operate over, calling member functions on the collection: std::vector<MyType> v; ... // vector is populated For calling functions with no arguments it's pretty straight-forward: std::for_each(v.begin(), v.end(), std::mem_fun(&MyType::myfunc)); A similar thing can be done if there's one...

What are you using to unit test your C++ code?

I'm looking into some possible options for unit testing C++ classes. So, short and to the point, what are you using? ...

How to serialize in c++ ?

Using java for 3 years and serialization / deserialization is something trivial. I would like to know whether c + + is also well? It is possible to serialize / deserialize a class in c + +? If someone can leave me an example would be a great help to me. EDIT : There are some native library that allows this? ...

Variable Naming Conventions in C++

Hi All, I come from a .NET world and I'm new to writting C++. I'm just wondering what are the preferred naming conventions when it comes to naming local variables and struct members. For example, the legacy code that I've inheritted has alot of these: struct MyStruct { TCHAR szMyChar[STRING_SIZE]; bool bMyBo...

Changing folder security permisions via Win32 API

My C++ application stores some common user data in %CSIDL_COMMON_APPDATA%\Company\Product. I want to make sure the Users group has write permisions to this folder which on Vista it does not. How would do I do this? ...

Do modern compilers optimize the x * 2 operation to x << 1?

Does the C++ compiler optimize this operation? I would love to believe that yes. ...

Simplest, safest way of holding a bunch of const char* in a set?

I want to hold a bunch of const char pointers into an std::set container [1]. std::set template requires a comparator functor, and the standard C++ library offers std::less, but its implementation is based on comparing the two keys directly, which is not standard for pointers. I know I can define my own functor and implement the operat...

C++ dynamic class ( dynamic hack )

Is there any way to add a field to a class at runtime ( a field that didn't exist before ) ? Something like this snippet : Myobject *ob; // create an object ob->addField("newField",44); // we add the field to the class and we assign an initial value to it printf("%d",ob->newField); // now we can access that field I don't really care h...

Using NaN in C++?

What's the best way to use NaNs in C++? I found std::numeric_limits<double>::quiet_NaN() and std::numeric_limits<double>::signaling_NaN(). I'd like to use signaling_NaN to represent an uninitialized variable as follows: double diameter = std::numeric_limits<double>::signaling_NaN(); This, however, signals (raises an exception) on as...

Is there a standard way to do findfirst, findnext with gcc on linux using stl?

I can't seem to find the _findfirst / findfirst, _findnext / findnext API on gcc for Linux, and would actually rather use the Standard Template Library (STL) for that if it is included there. Does anyone know what API there is available for listing files in a directory under Linux for C++ (gcc)? ...

Can the overall implementation of a component be divided in two objects?

I've seen with Microsoft COM and XPCOM, at least from what I've read and gathered so far, that the implementations of interfaces in a component have to essentially be in the single class that derives all the virtual interfaces. Is this correct? What am I missing? Is there a way to have multiple objects (possibly in separate DLL's) each ...

Multiple definitions of a function template

Suppose a header file defines a function template. Now suppose two implementation files #include this header, and each of them has a call to the function template. In both implementation files the function template is instantiated with the same type. // header.hh template <typename T> void f(const T& o) { // ... } // impl1.cc #in...

How to use std::signaling_nan?

After looking at another question on SO (Using NaN in C++) I became curious about std::numeric_limits<double>::signaling_NaN(). I could not get signaling_NaN to throw an exception. I thought perhaps by signaling it really meant a signal so I tried catching SIGFPE but nope... Here is my code: double my_nan = numeric_limits<double>::sig...

How do you throttle the bandwidth of a socket connection in C?

I'm writing a client-server app using BSD sockets. It needs to run in the background, continuously transferring data, but cannot hog the bandwidth of the network interface from normal use. Depending on the speed of the interface, I need to throttle this connection to a certain max transfer rate. What is the best way to achieve this, p...

Cancel libcurl easy handle

Is there an easy way to cancel a curl_easy_perform from another thread? ...

Unmanaged DLLs in C++

I've been reading many a tutorial/article on unmanaged DLLs in C++. For the life of me, however, I cannot seem to grasp the concept. I'm easily confused by the seeming disagreement about whether it needs a header file, how to export it, whether I need a .lib file and what have you. So, let's assume I have just a function like so: publi...