c++

How to create a QWidget with a HWND as parent?

With wxWidgets I use the following code: HWND main_window = ... ... wxWindow *w = new wxWindow(); wxWindow *window = w->CreateWindowFromHWND(0, (WXHWND) main_window); How do I do the same thing in Qt? The HWND is the handle of the window I want as the parent window for the new QtWidget. ...

How do I use std::tr1::mem_fun in Visual Studio 2008 SP1?

The VS2008 SP1 documentation talks about std::tr1::mem_fun. So why, when I try and use std::tr1::mem_fun, why do I get this compile error?: 'mem_fun' : is not a member of 'std::tr1' At the same time, I can use std::tr1::function without problems. Here is the sample code I am trying to compile, which is supposed to call TakesInt on a...

Why does this const member function allow a member variable to be modified?

class String { private: char* rep; public: String (const char*); void toUpper() const; }; String :: String (const char* s) { rep = new char [strlen(s)+1]; strcpy (rep, s); } void String :: toUpper () const { for (int i = 0; rep [i]; i++) rep[i] = toupper(rep[i]); } int main () { ...

How much work should be done in a constructor?

Should operations that could take some time be performed in a constructor or should the object be constructed and then initialised later. For example when constructing an object that represents a directory structure should the population of the object and its children be done in the constructor. Clearly, a directory can contain director...

Generic vector of vectors in C++

Is there a good way in C++ to implement (or fake) a type for a generic vector of vectors? Ignore the issue of when a vector of vectors is a good idea (unless there's something equivalent which is always better). Assume that it does accurately model the problem, and that a matrix does not accurately model the problem. Assume also that te...

How to prevent a new window from opening in hosted Internet Explorer

I'm hosting an Internet Explorer instance (the Web Control) in an CAxWindow ATL class. How do I prevent that a website could open a new window from the hosted IE? I'm already setting an implementation of IDocHostUIHandlerDispatch to disable the context menu. ...

Elegant Object comparison

When comparing two objects (of the same type), it makes sense to have a compare function which takes another instance of the same class. If I implement this as a virtual function in the base class, then the signature of the function has to reference the base class in derived classes also. What is the elegant way to tackle this? Should th...

What are some C++ related idioms, misconceptions, and gotchas that you've learnt from experience?

What are some C++ related idioms, misconceptions, and gotchas that you've learnt from experience? An example: class A { public: char s[1024]; char *p; A::A() { p = s; } void changeS() const { p[0] = 'a'; } }; Even know changeS is a const member function, it is changing the value of the object. So a cons...

tr1::mem_fn and members with default arguments...

I have class with a member function that takes a default argument. struct Class { void member(int n = 0) {} }; By means of std::tr1::mem_fn I can invoke it: Class object; std::tr1::mem_fn(&Class::member)(object,10); That said, if I want to invoke the callable member on the object with the default argument, what's the corre...

How do you call a constructor for global objects, for arrays of objects, and for objects inside classes/structs?

How would you call the constructor of the following class in these three situations: Global objects, arrays of objects, and objects contained in another class/struct? The class with the constructor (used in all three examples): class Foo { public: Foo(int a) { b = a; } private: int b; }; And here are my attem...

Read (and write) RTF files with C++ / Qt

Hello, I am looking for a simple C++ library for tokenizing and parsing RTF (Rich Text Format) files. I am planning to edit them with Qt's QTextEdit. More the Formatting preserved the better -- but actually I am planning to use Bold and Italics only. In perl I would use RTF::Tokenizer. It would be nice if the module had some sort of ...

What are some techniques for code generation?

I'm generating C++ code, and it seems like it's going to get very messy, even my simple generating classes already have tons of special cases. Here is the code as it stands now: http://github.com/alex/alex-s-language/tree/local%2Fcpp-generation/alexs_lang/cpp . ...

Where can I find с/с++ users journal archive in suitable format (not .html)?

I am searching for this magazine for quite a long time and I can`t find it. Any ideas? ...

Most elegant looping construct?

Sorry for the newb question. I'm still learning programming. So I'm using C++, and I need to do something like this: int n; do { n = get_data(); if(n != -1) send(n); } while(n != -1); This is just a sketch. Anyway it doesn't feel real elegant. I have to have my test twice. I could just test once and set a flag, bu...

Math factors game - who can write the most elegant solution?

Here's a fun exercise. http://www.hughchou.org/hugh/grid_game.cgi?CLEAR Who can write the most elegant yet performant code to win this game? My solution is in C/C++. #include<vector> #include<iostream> #include<fstream> #define N 64 std::vector<int> numbers(N+1); void init() { for(int i = 0; i < N+1; ++i) numbers[i] = 1; ...

Triangle problem

Hey!! I am trying to resolve Euler Problem 18 -> http://projecteuler.net/index.php?section=problems&amp;id=18 I am trying to do this with c++ (I am relearning it and euler problems make for good learning/searching material) #include <iostream> using namespace std; long long unsigned countNums(short,short,short array[][15],short,boo...

How to improve performance of an Abstract Factory when all the time appears to be spent in memory allocation.

The application de-serializes a stream into dynamically allocated objects and then keeps base type pointers in a linked list (i.e. abstract factory). It's too slow. Profiling says all the time is spent in operator new. Notes: The application already uses a custom memory allocator that does pooling. The compiler is VC++ 6.0 and the co...

References Needed for Implementing an Interpreter in C/C++

I find myself attached to a project to integerate an interpreter into an existing application. The language to be interpreted is a derivative of Lisp, with application-specific builtins. Individual 'programs' will be run batch-style in the application. I'm surprised that over the years I've written a couple of compilers, and several dat...

Does delete work with pointers to base class?

Do you have to pass delete the same pointer that was returned by new, or can you pass it a pointer to one of the classes base types? For example: class Base { public: virtual ~Base(); ... }; class IFoo { public: virtual ~IFoo() {} virtual void DoSomething() = 0; }; class Bar : public Base, public IFoo { public: vi...

Using sprintf without a manually allocated buffer

In the application that I am working on, the logging facility makes use of sprintf to format the text that gets written to file. So, something like: char buffer[512]; sprintf(buffer, ... ); This sometimes causes problems when the message that gets sent in becomes too big for the manually allocated buffer. Is there a way to get sprint...