c++

C++0x: how to ask for a small addition? (syntax of pure virtual functions)

Hi all. In the current C++0x draft I've noticed they introduced some new explicit keywords to highlight expected behaviors (great move!). Examples: defaulted/deleted functions (= default and = delete), the new nullptr constant, the explicit keyword usable also for conversion operators, ... So I expected to see also a = pure syntax for...

Version resource in DLL not visible with right-click

I'm trying to do something which is very easy to do in the regular MSVC, but not supported easily in VC++ Express. There is no resource editor in VC++ Express. So I added a file named version.rc into my DLL project. The file has the below content, which is compiled by the resource compiler and added to the final DLL. This resource is vi...

Is "boolean short circuiting" dictated by standard or just mostly used as optimization?

EDIT: Found duplicate once I learned the term for this behaviour. Close as duplicate please. Consider this Class* p = NULL; if( p != NULL && p->Method() == OK ){ // stuff } On all compilers I've worked with, this is quite safe. I.e. the first part of the boolean expression will evaluate to false, and the call to Method() will thus...

How to know when a new USB storage device is connected in Qt?

I want to know when a USB device is connected to the computer that my Qt application is running on (in Windows). In my main QWidget, I've reimplemented winEventFilter like this: bool winEventFilter ( MSG * msg, long * result ) { qDebug() << msg; return false; } I'd expect qDebug to send at least something when I connect a USB ...

Win32, C++: Creating a popup window without stealing focus

I am creating a program that displays a popup at certain times (just like some chat clients for example) on which the user can click. However, I do not want to take away the focus from the current application. The way I'm doing it now is by using a HWND with WS_POPUPWINDOW and minimizing and then restoring the window. However, this ste...

Code generation tool targeting C++ and C#

I have a set of applications that are being built using a combination of C# and C++. We have a set of shared objects between the two languages, and rather than define each one separately in each language, I would prefer to use a code generation tool. Ideally such a tool would be FOSS, although that's not an absolute requirement. The o...

How do I find the elements of an array in C++ which sum to a value greater than or equal to a specific number?

I've written a loop in C++ to give me 6 random numbers and store them in an array. What I would like to do is to sum the elements of the array until I get a value larger than a number, "x", but I would like to do this without necessarily adding all the elements. The objective is to find the first elements which sum to the value of x. ...

Windows volume device detect failed until reboot. Never failed before.

I have code to detect the connection of USB Flash Drives as volumes. The code has been working very well for awhile, but recently a fellow engineer's machine started to fail and didn't work right again until it was restarted. The project uses Qt 4.5.0, but that shouldn't be very relevant to this question. I register for the notificati...

Is Critical Section always faster ?

I was debugging a multi-threaded application and found the internal structure of CRITICAL_SECTION. I found data member LockSemaphore of CRITICAL_SECTION an interesting one. It looks like LockSemaphore is an auto-reset event (not a semaphore as the name suggests) and operating system creates this event silently when first time a thread ...

Underlying type of a C++ enum in C++0x

I've been trying to read a bit of the C++ standard to figure out how enum's work. There's actually more there than I originally thought. For a scoped enumeration, it's clear that the underlying type is int unless otherwise specified with an enum-base clause (it can be any integral type). enum class color { red, green, blue}; // these ...

What memory management do I need to cleanup when using TinyXml for C++?

I'm doing the following with TinyXml: TiXmlDocument doc; TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" ); TiXmlElement* main = new TiXmlElement("main"); TiXmlElement* header = new TiXmlElement("header"); header->SetAttribute("attribute","somevalue"); main->LinkEndChild(header); // ... Add many more TiXmlElment* to other...

Programmatically get processor details from Mac OS X

My application running on Mac OS X that needs to retrieve details about the machine it is running on to report for system information. One of the items I need is details about the processor(s) installed in the computer. My code currently works, but is far from an ideal solution, in fact I consider it a bad solution, but I have had no l...

Locking files using C++ on Windows

I have a program writing/reading from a file, and I want to lock the file for other instances of my application. How can I do it (in c++ visual studio 2003)? I tried using the _locking() but then also I myself cannot reach the file when trying to read/write (in the same instance). I know there's an option of LockFile() but have no idea ...

Layout of Pixel-data in Memory?

I'm writing a C++ library for an image format that is based on PNG. One stopping point for me is that I'm unsure as to how I ought to lay out the pixel data in memory; as far as I'm aware, there are two practical approaches: An array of size (width * height); each pixel can be accessed by array[y*width + x]. An array of size (height), ...

C++ compiler error in netbeans.

I've tried everything from reading the Netbeans help to browsing Google. This code works fine in Dev-Cpp but not Netbeans 6.5.1. Netveans also places and exclamation mark next to #include <iostream> which i checked and is in the include path of netbeans and is in the include folder: #include <iostream> int main() { std::cout << "Te...

When and why is an std::__non_rtti_object exception generated?

I'm using Visual Studio and performing a valid dynamic cast. RTTI is enabled. Edit : Updated the code to be more realistic struct base { virtual base* Clone() { base* ptr = new base; CopyValuesTo( ptr ); return ptr; } virtual void CopyValuesTo( base* ptr ) { ... } virtual ~ba...

How do I access internal members of a union?

I have a union that is defined like this: typedef union { enum { REVISION = 0, CURRENT_VERSION = REVISION }; enum FLAGS{ FLAG_DEFAULT = 0x00000000, FLAG_EOD = 0x00000001, FLAG_OUTOFORDER = 0x00000002 }; CHAR _filler[32]; struct INTERNAL_STRUCTURE { UINT16 ...

Why is the use of tuples in C++ not more common?

Why does nobody seem to use tuples in C++, either the Boost Tuple Library or the standard library for TR1? I have read a lot of C++ code, and very rarely do I see the use of tuples, but I often see lots of places where tuples would solve many problems (usually returning multiple values from functions). Tuples allow you to do all kinds o...

C++ Custom Enum Struct for INI file reader

I'm trying to create an Enum that has a string label and a value and I plan to use this to read stuff from an ini file. For example in the ini file I might have some double, int or string type values preceded by the tag/name of the value: SomeFloat = 0.5 SomeInteger = 5 FileName = ../Data/xor.csv When I read the tag from a file it co...

Is it possible to make a factory in C++ that complies with the open/closed principle?

In a project I'm working on in C++, I need to create objects for messages as they come in over the wire. I'm currently using the factory method pattern to hide the creation of objects: // very psuedo-codey Message* MessageFactory::CreateMessage(InputStream& stream) { char header = stream.ReadByte(); switch (header) { case ...