c++

Instantiating classes by name with factory pattern

Suppose I have a list of classes A, B, C, ... which all inherit from Base. I get the class name as a string from the user, and I want to instantiate the right class and return a pointer to Base. How would you implement this? I thought of using a hash-table with the class name as the key, and a function pointer to a function that instan...

Is Network Up? C++ Fedora/Unix

Does any one have a snippet of their code that, checks if the network is enabled on a machine and has an active IP Address. I have a networking software that connects to other client machines, Although it works when the machine is connected but if i unplug the cable or disable the network, It throws a whole reem of exceptions. It woul...

Can I use C++ templates to generate Unicode/ANSI variants of a function, rather than using the preprocessor?

We've got a bunch of legacy code that doesn't support Unicode, so a transitional pattern we use in our code is to move the function to a .inl file, change char to CHAR_TYPE, and then wrap it up like this: #define CHAR_TYPE wchar_t #define STRING_TYPE std::wstring #define MyFunctionName MyFunctionNameW #include "MyFunction.inl" #undef C...

boost::any test code compiles with Sun CC but not g++

The following noddy test code: #include <iostream> #include <list> #include <boost/any.hpp> #include <boost/foreach.hpp> #include <typeinfo.h> using boost::any_cast; using std::cout; using std::cerr; typedef std::list<boost::any> many; template <typename T> inline bool is_any(const boost::any& op) { return (op.type() == typeid(T)); ...

How do I avoid popping up an error dialog when my MSVS C++ app crashes

When my Visual Studio 2008 C++ command-line application crashes, it sometimes produces this dialog. CommandProcessor.exe has encountered a problem and needs to close. We are sorry for the inconvenience. If you were in the middle of something, the information you were working on might be lost. For more informaiton about this error, clic...

C++ CPU Register Usage

In C++, local variables are always allocated on the stack. The stack is a part of the allowed memory that your application can occupy. That memory is kept in your RAM (if not swapped out to disk). Now, does a C++ compiler always create assembler code that stores local variables on the stack? Take, for example, the following simple code:...

Default assignment operator in inner class with reference members

I've run into an issue I don't understand and I was hoping someone here might provide some insight. The simplified code is as follows (original code was a custom queue/queue-iterator implementation): class B { public: B() {}; class C { public: int get(); C(B&b) : b(b){}; private: B& b; }; ...

How to catch divide-by-zero error in Visual Studio 2008 C++?

How can I catch a divide-by-zero error (and not other errors; and to be able to access exception information) in Visual Studio 2008 C++? I tried this: try { int j=0; int i= 1/j;//actually, we call a DLL here, which has divide-by-zero } catch(std::exception& e){ printf("%s %s\n", e.what()); } catch(...){ printf("generic except...

two dimensional array using vector in cpp

Hi, I want to create 2D array using vector. But, when I do this, I get seg fault. Can anyone please explain what I am doing wrong, and possible solution for this problem. I made everything public since I dont want to deal with getters and setters now. I want to get the concept of 2D array clear. Thanks. #include <iostream> #include <v...

operator/ overloading

For learning purposes I'm creating big integer class in C++. There are 2 files: big_int.h #ifndef BIG_INT_H #define BIG_INT_H #include class big_int { public: big_int(void); big_int(char*); big_int(QString); ~big_int(); big_int operator+(big_int); big_int operator-(big_int); big_int operator*(big_int)...

Not able execute CreateProcess with PhotoViewer.dll

In my application there is an interface where user can select any file and open in its default application depending on the file association. I am using FindExecutable and CreateProcessAsUser with Explorer token. Now the problem is in the case of picture files say .jpg, FindExecutable returns "C:\Program Files\Windows Photo Gallery\P...

Detach a pointer from a shared_ptr?

A function of my interface returns a pointer to an object. The user is supposed to take ownership of that object. I do not want to return a Boost.shared_ptr, because I do not want to force clients to use boost. Internally however, I would like to store the pointer in a shared_ptr to prevent memory leaks in case of exceptions etc. There s...

What is the correct way to initialize a Matrix in C++98?

I'm not programmer I want to compile Inkscape in win32, and stumbled on these error messages: ui/dialog/filedialogimpl-win32.cpp:1379: error: in C++98 'matrix' must be initialized by constructor, not by '{...}' make[1]: *** [ui/dialog/filedialogimpl-win32.o] Error 1 The suspected code of filedialogimpl-win32.cpp: ... // D...

C macro to transform a SVN revision to an integer

I am looking for a C/C++ macro that can transform a random SVN revision like "$Revision: 9 $" or "$Revision: 9999999 $" into an integer or a string. I know that simple functions exists to achieve this, but I want this to be made at compile time. My wish is to write things like:unsigned int rev = SVN_TO_INT("$Revision$"); ...

A good example for boost::algorithm::join

I recently wanted to use boost::algorithm::join but I couldn't find any usage examples and I didn't want to invest a lot of time learning the Boost Range library just to use this one function. Can anyone provide a good example of how to use join on a container of strings? Thanks. ...

C++ frontend only compiler (convert C++ to C).

I'm currently managing some C++ code that runs on multiple platforms from a single source tree (Win32, Linux, Verifone CC terminals, MBED and even the Nintendo GBA/DS). However I need to build an app targetted at an embedded platform for which there is no C++ compiler (C only). I remmber that many of the early C++ compilers were only fro...

Boost: how to build Boost under MacOSX

I am trying to build MacOSX universal binaries (I need at least i386/ppc for >=macosx10.3) of Boost. I tried a lot of different methods and options and versions and it all fails in the end with this crash: http://stackoverflow.com/questions/1823605/boost-what-could-be-the-reasons-for-a-crash-in-boostslotslot I guess this crash is beca...

In C++, is there a difference between “throw” and “throw ex”?

I'd like to ask this question (also here), but this time about C++. What is the difference in C++ between try { /*some code here*/} catch(MyException& ex) { throw ex;} and try { /*some code here*/} catch(MyException& ex) { throw;} Is it just in the stack trace (which in C++ is in any case not a standard as in C# or Java)? (If i...

Access violation writing location

I have the following code: #include <openssl/bn.h> #include <openssl/rsa.h> unsigned char* key; RSA* rsa = RSA_new(); rsa = RSA_generate_key(1024,65537,NULL,NULL); //init pubkey key[BN_num_bytes(rsa->n)] = '\0'; BN_bn2bin(rsa->n, key); printf("RSA Pub: %s\n", key); RSA_free( rsa ); rsa = NULL; The debugger is telling me that I have a...

OpenCV and Xcode

I am looking at pattern recognition in images and video and have thought that C++ is the way to go (for high performance/real-time applications). I want to use the OpenCV libraries in Xcode when I build a C++ application (command-line tool) I am running Xcode 3.2 and have just followed the steps at http://opencv.willowgarage.com/wiki/Ma...