c++

Calling constructors in c++ without new

I've often seen that people create objects in C++ using Thing myThing("asdf"); Instead of Thing myThing = myThing("asdf"); This seems to work (using gcc), at least as long as there are no templates involved. My question now, is the first line correct and if so should I use it? ...

C++ resize a docked Qt QDockWidget programmatically?

I've just started working on a new C++/Qt project. It's going to be an MDI-based IDE with docked widgets for things like the file tree, object browser, compiler output, etc. One thing is bugging me so far though: I can't figure out how to programmatically make a QDockWidget smaller. For example, this snippet creates my bottom dock window...

Generalized plugable caching pattern?

Given that it's one of the hard things in computer science, does anyone know of a way to set up a plugable caching strategy? What I'm thinking of would allow me to write a program with minimal thought as to what needs to be cached (e.i. use some sort of boiler-plate, low/no cost pattern that compiles away to nothing anywhere I might wa...

Create object of unknown class (two inherited classes)

I've got the following classes: class A { void commonFunction() = 0; } class Aa: public A { //Some stuff... } class Ab: public A { //Some stuff... } Depending on user input I want to create an object of either Aa or Ab. My imidiate thought was this: A object; if (/*Test*/) { Aa object; } else { Ab object; } Bu...

How to detect if the Windows DWORD_PTR type is supported, using an ifdef?

There are some new integer types in the Windows API to support Win64. They haven't always been suppoprted; e.g. they aren't present in MSVC6. How can I write an #if condition to detect if these types are supported by <windows.h>? (My code needs to compile under many different versions of Microsoft Visual C++, including MSVC6. So I ne...

C++ methods which take templated classes as argument.

I have a templated class Vector<class T, int N> Where T is the type of the components (double for example) and n the number of components (so N=3 for a 3D vector) Now I want to write a method like double findStepsize(Vector<double,2> v) {..} I want to do this also for three and higher dimensional vectors. Of course I could just i...

Starting a C++ project. Should I worry about freeing dynamic allocated memory?

I am pretty proficient with C, and freeing memory in C is a must. However, I'm starting my first C++ project, and I've heard some things about how you don't need to free memory, by using shared pointers and other things. Where should I read about this? Is this a valuable replacement for proper delete C++ functionality? How does it work...

How to debug packet loss ?

I wrote a C++ application (running on Linux) that serves an RTP stream of about 400 kbps. To most destinations this works fine, but some destinations expericence packet loss. The problematic destinations seem to have a slower connection in common, but it should be plenty fast enough for the stream I'm sending. Since these destinations a...

c++ memcpy return value

according to http://www.cplusplus.com/reference/clibrary/cstring/memcpy/ c++'s memcpy takes three parameters: destination, source and size/bytes. it also returns a pointer. why is that so? aren't the parameters enough to input and copy data. or am i misunderstanding something? the examples don't use the return value ...

C++ Pointer to a function which takes an instance of a templated class as argument

I have trouble compiling a class, which has function pointers as member variables. The pointers are to functions which take an instance of a class as argument. Like template<class T, int N> double (*f)(Vector<T,N> v); I get "error: data member 'f' cannot be a member template" Compiler is gcc 4.2. Edit Before using templates I just ...

Can't link Hello World!

Guys that is code copied from a book (Programming Windows 5th edition): #include <windows.h> int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { MessageBox (NULL, TEXT ("Hello, Windows 98!"), TEXT ("HelloMsg"), 0) ; return 0 ; } Link to the topic in which th...

OpenGL texture randomly not shown

Hello, I have got a very, very strange problem in my C++ OpenGL application. I simply load a texture and apply it to a quadric: glGenTextures(1, &tex); glBindTexture(GL_TEXTURE_2D, tex); glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEARE...

Is this not downcasting?

If I do double d = 34.56; int i = (int)d; Am I not "downcasting"? OR Is this term only used in terms of classes and objects? I am confused because in this case we are "downcasting" from a bigger double to a smaller int, but in case of classes, we "downcast" from a smaller base class to a bigger derived class. Aren't these two c...

Improving File Read Performance (single file, C++, Windows)

I have large (hundreds of MB or more) files that I need to read blocks from using C++ on Windows. Currently the relevant functions are: errorType LargeFile::read( void* data_out, __int64 start_position, __int64 size_bytes ) const { if( !m_open ) { // return error } else { seekPosition( start_position ); DWORD bytes_read; BOOL...

Multi color Edit Field (Win32)

I want to create a program that will parse text for key words and make these words a certain color. What type of control supports many different colors? Would I have to create my own, or override the OnPaint() of a basic control or something? (Id like to avoid making my own control from scratch) Thanks ...

Qt 4.x: how to implement drag-and-drop onto the desktop or into a folder?

Hi all, I've written a little file-transfer application written in C++ using Qt 4.x ... it logs into a server, shows the user a list of files available on the server, and lets the user upload or download files. This all works fine; you can even drag a file in from the desktop (or from an open folder), and when you drop the file icon in...

C++: Efficiently adding integers to strings

I know how to add integers to strings, but I'm not sure I'm doing it in an efficient matters. I have a class where I often have to return a string plus an integer (a different integer each time), in Java I would do something like public class MyClass { final static String S = "MYSTRING"; private int id = 0; public String getStrin...

Reading File in C++ throwing Seg Fault

Hello, When trying to read tokens from a file in C++, I receive a seg fault. Just to play with some things, I tried reading over the file and just printing and surprisingly if you uncomment the code for the first file read and then read the second file everything works fine. If you leave the commented code commented, you receive the seg...

Are there any modern platforms with non-IEEE C/C++ float formats?

Hi all, I am writing a video game, Humm and Strumm, which requires a network component in its game engine. I can deal with differences in endianness easily, but I have hit a wall in attempting to deal with possible float memory formats. I know that modern computers have all a standard integer format, but I have heard that they may not...

protecting COM interfaces from exceptions

I have several dozen objects exposed through COM interfaces, each of which with many methods, totaling a few hundred methods. These interfaces expose business objects from my app to a scripting engine. I have been given the task of protecting every single one of these methods from exceptions being thrown (to catch them and return an er...