c++

Visual Studio C++ compiler optimizations breaking code?

I've a peculiar issue here, which is happening both with VS2005 and 2010. I have a for loop in which an inline function is called, in essence something like this (C++, for illustrative purposes only): inline double f(int a) { if (a > 100) { // This is an error condition that shouldn't happen.. } // Do something with a and r...

How to re-engineer an Ui-File from a given QWidget instance

Does anybody know if the Qt Toolkit provides a way to generate Ui Files from a given QWidget instance? Speaking in pseudo-code, I'm looking for something like this: //setup widget QWidget* pMyWidget=new QWidget(...); //fill widget with life pMyWidget->layout()->addWidget(new QLabel(...)); ... //finally write a Ui file: pMyWidget->expo...

Python ctypes, C++ object destruction

Consider the following python ctypes - c++ binding: // C++ class A { public: void someFunc(); }; A* A_new() { return new A(); } void A_someFunc(A* obj) { obj->someFunc(); } void A_destruct(A* obj) { delete obj; } # python from ctypes import cdll libA = cdll.LoadLibrary(some_path) class A: def __init__(self): self.obj...

Closing a MessageBox automatically

I have a third party encryption library, which may create a MessageBox if key creation fails. The failure can be caused by bad random number generation or other rarities, and in most cases, trying again will result in success. My code will attempt key creation up to three times before deciding it failed. Now, the issue is that the progr...

Play avi using animation control

Hi, I,m not c++ programmer on a daily basis, so I need help. I 'wrote' this. It's new project 'Windows Application' in DevC++. I add this #include <Commctrl.h> //... HWND film; //... film = Animate_Create(hwnd, 10, WS_CHILD | WS_VISIBLE | ACS_AUTOPLAY, hThisInstance); Animate_OpenEx(film, hThisInstance, "a.avi"); Animate_Play(film, 0...

[C2664] cannot convert parameter 1 from 'overloaded-function' to '...'

Now I am try to use boost bind & mem_fn. But there's a problem to bind overloaded-function. How to resolve compile error of follow codes? boost::function< void( IF_MAP::iterator ) > bmf = std::mem_fun1< void, IF_MAP, IF_MAP::iterator >( &IF_MAP::erase ); boost::function< void( IF_MAP::iterator ) > bmf = boost::mem_fn< void, IF_MAP, IF_M...

C++ 2.5 bytes (20-bit) integer

I know it's ridiculous but I need it for storage optimization. Is there any good way to implement it in C++ ? It has to be flexible enough so that I can use as normal data type e.g Vector< int20 >, operator overloading etc.. ...

Memcpy int to char buffer - as alternative to sprintf

#include <iostream> using namespace std; int main() { char buffer[8]; int field=534; memcpy(buffer,&field,sizeof(field)); cout<<buffer<<endl; return 0; } This returns an empty buffer. Why? Basically looking for an alternative to sprintf to convert int to char buffer. Itoa is not available. Thoughts? Bet...

Class method as winAPI callback

Is it feasible to set the winAPI message callback function as a method of a class. If so, how would this be best implemented? I wonder if it is even possible. Sorry for the short question, hopefully you will be able to provide useful responses. Thanks in advance :). ...

1-D into 2-D array

A program in c++ that accept an integer array and its size as arguments and assign the elements into a 2-D array of integers. for ex: if the array is 1,2,3 The resultant 2-D array is given by 1 2 3 1 2 0 1 0 0 ...

How do I detect "_" in a C++ string?

Hello, I want to know the positions of the "_" in a string: string str("BLA_BLABLA_BLA.txt"); Something like: string::iterator it; for ( it=str.begin() ; it < str.end(); it++ ){ if (*it == "_") //this goes wrong: pointer and integer comparison { pos(1) = it; } cout << *it << endl; } Thanks, André ...

How to encapsulate a C API into RAII C++ classes?

Given a C API to a library controlling sessions that owns items, what is the best design to encapsulate the C API into RAII C++ classes? The C API looks like: HANDLE OpenSession(STRING sessionID); void CloseSession(HANDLE hSession); HANDLE OpenItem(HANDLE hSession, STRING itemID); void CloseItem(HANDLE hItem); Plus other functions th...

Simple c++ pointer casting

Can someone explain this to me: char* a; unsigned char* b; b = a; // error: invalid conversion from ‘char*’ to ‘unsigned char*’ b = static_cast<unsigned char*>(a); // error: invalid static_cast from type ‘char*’ to type ‘unsigned char*’ b = static_cast<unsigned char*>(static_cast<void*>(a)); // everything is fine What makes the dif...

Books about/for deeper understanding debugging C or C++ (assembly) on Windows?

I'm a professional C++ programmer and I'm pretty OK with the Visual Studio debugger (currently using 2005). I halfway know what a processor register is supposed to be, I can make something of a disassembly view (mostly guessing at what individual asm instructions might do). I know about symbol files -- that is their existence and how to ...

Another problem with templates.

#include "stdafx.h" #include <iostream> using std::cout; template<class T> class IsPolymorphic { template<class T> struct Check { enum {value = false}; }; template<class T> struct Check<T*> { enum {value = true}; }; public: enum {value = Check<T>::value}; }; template<bool flag, class T, class U> struct Select { typedef...

how can I use static library build by different version of mingw?

Greetings, I am facing a complicated situation about using a static library under windows. The static library is build by a specific version of mingw which is bundled with Eiffel studio. Since Eiffel studio uses mingw to create its output as a static lib, I have no control over this configuration. If I try to use this static library w...

Is a logical right shift by a power of 2 faster?

Hi there, I would like to know if performing a logical right shift is faster when shifting by a power of 2. I am using C++. For example, is myUnsigned >> 4 any faster than myUnsigned >> 3 I appreciate that everyone's first response will be to tell me that one shouldn't worry about tiny little things like this, it's using correct a...

Can I pass a constructor to a function?

I want to instantiate a base class pointer to point to a newly constructed derived class object. The actual class for the object will change depending on the application type, so I want to use a Factory method to switch on certain variables in order to construct the right object. However I don't want to have to do the switch each time. ...

How can I add a static method to a QInputDialog for returning custom data?

QString QInputDialog::getText ( QWidget * parent, const QString & title, const QString & label, QLineEdit::EchoMode mode = QLineEdit::Normal, const QString & text = QString(), bool * ok = 0, Qt::WindowFlags flags = 0 ) [static] This function is defined to call a dialog get and return the text which will be inserted in ...

Using boost::lock_guard for simple shared data locking

I am a newcomer to the Boost library, and am trying to implement a simple producer and consumer threads that operate on a shared queue. My example implementation looks like this: #include <iostream> #include <deque> #include <boost/thread.hpp> boost::mutex mutex; std::deque<std::string> queue; void producer() { while (true) { ...