c++

What is the best way to only include certain libraries on certain operating systems in c/c++?

When writing an app that one wants to have compile on mac, linux and windows, what is the best way of managing the different libraries that will need to be included on the various operating systems. For example, using the glut opengl toolkit requires different includes on each operating system. ...

data access object pattern implementation

Hi all, I would like to implement a data access object pattern in C++, but preferably without using multiple inheritance and/or boost (which my client does not like). Do you have any suggestions? ...

Problem Linking "static" Methods in C++

I want to call a few "static" methods of a CPP class defined in a different file but I'm having linking problems. I created a test-case that recreates my problem and the code for it is below. (I'm completely new to C++, I come from a Java background and I'm a little familiar with C.) // CppClass.cpp #include <iostream> #include <pthrea...

using fstream to read every character including spaces and newline

I wanted to use fstream to read a txt file. I am using inFile >> characterToConvert, but the problem is that this omits any spaces and newline. I am writing an encryption program so i need to include the spaces and newlines and was wondering what would be the proper way to go about accomplishing this. Thanks in advance, Tomek ...

When have we any practical use for hierarchical namespaces in c++?

I can understand the use for one level of namespaces. But 3 levels of namespaces. Looks insane. Is there any practical use for that? Or is it just a misconception? ...

design by contract tests by assert or by exception?

When programming by contract a function or method first checks whether its preconditions are fulfilled, before starting to work on its responsibilities, right? The two most prominent ways to do these checks are by assert and by exception. assert fails only in debug mode. To make sure it is crucial to (unit) test all separate contract ...

Use of 'const' for function parameters

How far do you go with const? Do you just make functions const when necessary or do you go the whole hog and use it everywhere? For example, imagine a simple mutator that takes a single boolean parameter: void SetValue(const bool b) { my_val_ = b; } Is that const actually useful? Personally I opt to use it extensively, including pa...

C++ timing, milliseconds since last whole second

I'm working on a C++ application that needs detailed timing information, down to the millisecond level. We intend to gather the time to second accuracy using the standard time() function in . We would like to additionally gather the milliseconds elapsed since the last second given by time(). Does anyone know a convenient method fo...

Inputting large numbers in c++?

What's the best way to handle large numeric inputs in c++ (eg 10^100)? For algorithms I usually switch over to ruby and I sometimes use strings. Any other good methods? ...

How to initialize Pango under Win32?

Having downloaded Pango and GLib from the GTK+ Project's Win32 downloads page and having created and configured a Win32 project under Visual Studio 2005 so it points to the proper lib and include directories, how do you initialize Pango for rendering to a Win32 window? Should the first call be to pango_win32_get_context()? Calling that ...

nonvirtual interface idiom for more than two levels of inheritance?

The non-virtual interface idiom describes how the virtual methods are nonpublic customisation points, and public methods are nonvirtual to allow the base class to control at all times how the customisation points are called. This is an elegant idiom and I like to use it, but how does it work if the derived class is a base class in itse...

Is there a faster way of getting a char* from a _variant_t than (const char*)(_bstr_t)

Here's the code I want to speed up. It's getting a value from an ADO recordset and converting it to a char*. But this is slow. Can I skip the creation of the _bstr_t? _variant_t var = pRs->Fields->GetItem(i)->GetValue(); if (V_VT(&var) == VT_BSTR) { char* p = (c...

Best method for storing this pointer for use in WndProc

I'm interested to know the best / common way of storing a this pointer for use in the WndProc. I know of several approaches, but each as I understand it have their own drawbacks. My questions are: What different ways are there of producing this kind of code: CWindow::WndProc(UINT msg, WPARAM wParam, LPARAM) { this->DoSomething(); } ...

Converting string of 1s and 0s into binary value

I'm trying to convert an incoming sting of 1s and 0s from stdin into their respective binary values (where a string such as "11110111" would be converted to 0xF7). This seems pretty trivial but I don't want to reinvent the wheel so I'm wondering if there's anything in the C/C++ standard libs that can already perform such an operation? ...

Best practice for translating exceptions in C++/CLI wrapper class

I am writing a .NET wrapper class for an existing native class which throws exceptions. What are the best practices for translating between native C++ exceptions and Managed exceptions? Catch and re-throw on a one-to-one basis (e.g. std::invalid_argument -> System.System.ArgumentException)? Is there a mapping already drawn up somewhere? ...

C++ Thread, shared data

I have an application where 2 threads are running... Is there any certanty that when I change a global variable from one thread, the other will notice this change? I don't have any syncronization or Mutual exclusion system in place... but should this code work all the time (imagine a global bool named dataUpdated): Thread 1: while(1) ...

Creating a ZIP file on Windows (XP/2003) in C/C++

I am looking for a way to create a ZIP file from a folder in Windows C/C++ APIs. I can find the way to do this in VBScript using the Shell32.Application CopyHere method, and I found a tutorial explaining how to do it in C# also, but nothing for the C API (C++ is fine too, project already uses MFC). I'd be really grateful if anyone can s...

What is the best signature for overloaded arithmetic operators in C++?

I had assumed that the canonical form for operator+, assuming the existence of an overloaded operator+= member function, was like this: const T operator+(const T& lhs, const T& rhs) { return T(lhs) +=rhs; } But it was pointed out to me that this would also work: const T operator+ (T lhs, const T& rhs) { return lhs+=rhs; } I...

How do I use Qt and SDL together?

I am building a physics simulation engine and editor in Windows. I want to build the editor part using Qt and I want to run the engine using SDL with OpenGL. My first idea was to build the editor using only Qt and share as much code with the engine (the resource manager, the renderer, the maths). But, I would also like to be able to run...

Compile errors in mshtml.h compiling with VS2008.

I'm in the process of moving one of our projects from VS6 to VS2008 and I've hit the following compile error with mshtml.h: 1>c:\program files\microsoft sdks\windows\v6.0a\include\mshtml.h(5272) : error C2143: syntax error : missing '}' before 'constant' 1>c:\program files\microsoft sdks\windows\v6.0a\include\mshtml.h(5275) : error C214...