c++

Should objects delete themselves in C++?

I've spent the last 4 years in C# so I'm interested in current best practices and common design patterns in C++. Consider the following partial example: class World { public: void Add(Object *object); void Remove(Object *object); void Update(); } class Fire : Object { public: virtual void Update() { if(age ...

Beginner question about Caesar cipher in C++

To start off, I'm four weeks into a C++ course and I don't even know loops yet, so please speak baby talk? Okay, so I'm supposed to read a twelve character string (plus NULL makes thirteen) from a file, and then shift the letters backwards three, and then print my results to screen and file. I'm okay with everything except the shifting...

Is this a reasonable use of the ternary operator?

Are there any understanding / maintainability issues that result from code like inVar1 == 0 ? NULL : v.push_back(inVar1); inVar2 == 0 ? NULL : v.push_back(inVar2); and so forth. The possibly confusing idea is using the ternary operator for program flow rather than variable assignment, which is the usual explanation. I haven't seen c...

Creating variables / vectors based on runtime

I am working on a program that "encodes" a file based on a supplied bookfile. The resulting file has each letter replaced with a number. This number corresponds to the offset of that letters appearence in the bookfile. So if we had "hello" it would pick an 'h' from the bookfile, find its location number and replace it in the output. Min...

How do I limit an external DLL to one CPU?

I have a program that I would like to run on just one CPU so it doesn't take up too much system resources. The problem is, it makes a call into an external DLL that automatically uses all available CPU cores. I do not have the source code to the external DLL. How can I limit the DLL to only using one CPU? EDIT: Thanks for the help, here...

C/C++ check if one bit is set in, i.e. int variable

int temp = 0x5E; // in binary 0b1011110. Is there such a way to check if bit 3 in temp is 1 or 0 without bit shifting and masking. Just want to know if there is some built in function for this, or am I forced to write one myself. ...

C++0x atomic template implementation

I know that similar template exits in Intel's TBB, besides that I can't find any implementation on google or in Boost library. ...

How do you serialize an object in C++?

I have a small hierarchy of objects that I need to serialize and transmit via a socket connection. I need to both serialize the object, then deserialize it based on what type it is. Is there an easy way to do this in C++ (as there is in Java)? Are there any C++ serialization online code samples or tutorials? EDIT: Just to be clear, I...

Overloading . -> and :: for use in multiplatform classes

Say I have three window classes, one for each OS I want to support: WindowsWindow OSXWindow LinuxWindow They all inherit from the Window class. This is also the class you instantiate. The Window class have the . -> and :: operators overloaded, and depending on which OS were running on (based on IFDEFs) it casts the this pointer to ...

Which c++ sites do you prefer?

just a simple question ... ist there a special board/forum .. or a blog you like . I'm new to c++ and i'd like to get deep in the matter :) so : what are the best c++ sites on this planet ? :) ...

Enum bitfield container class

Im trying to write a small class to better understand bit flags in c++. But something isnt working out. It prints the wrong values. Where is the problem? Have I misunderstood how to add flags? Or check if the bit field has them? Heres the code: #include <iostream> enum flag { A = 1, B = 2, C = 4 }; class Holder { public: Hold...

Get icons for common file types

Hi I want to get the icons of common file types in my dll. I am using vc++. I only have the file extension and mime type of the file based on which I want to get the icon for the file. Can someone please tell me how I can do that? (The method available in vc++ needs the user to give the path of the file for which the icon is needed. I ...

How to store a hash table in a file?

How can I store a hash table with separate chaining in a file on disk? Generating the data stored in the hash table at runtime is expensive, it would be faster to just load the HT from disk...if only I can figure out how to do it. Edit: The lookups are done with the HT loaded in memory. I need to find a way to store the hashtable (in m...

Need way to determine whether function has void return type in VC6 and VC7

The following C++ code compiles and runs correctly for GNU g++, LLVM and every other C++ compiler I threw at it except for Microsoft VC6 and VC7: template<typename A, typename B> int HasVoidReturnType(A(*)(B)) { return 0; } template<typename B> int HasVoidReturnType(void(*)(B)) { return 1; } void f(double) {} int foo() { return HasVoidR...

Simple GUI IDE?

Hi I'm looking for a GUI Linux IDE. Specs: simple and fool proof. normal look & feel full as-you-type indentation in most languages a compile+run button, a debugger, auto-refactoring for C++ basic unintrusive support for common buildsystems - straight make, cmake, qmake, autotools smooth workflow. proper keyboard support, no jarring ...

Is this code threadsafe?

I'm writing some code where the UI thread need to communicate with the background thread doing network communication. The code works, but would it be considered thread safe? I would feel a lot better if someone experienced could lead me on to the right path on this... static Mutex^ mut_currentPage = gcnew Mutex; static array<unsigned c...

Most suitable asynchronous socket model for an instant messenger client?

I'm working on an instant messenger client in C++ (Win32) and I'm experimenting with different asynchronous socket models. So far I've been using WSAAsyncSelect for receiving notifications via my main window. However, I've been experiencing some unexpected results with Winsock spawning additionally 5-6 threads (in addition to the initial...

creating an ostream

I am trying to create a c++ ostream for educational reasons. My test will be creating an ostream that acts like a ofstream would except instead of writing to a file it would write to a deque or vector container. ...

Regular expression to detect semi-colon terminated C++ for & while loops

In my Python application, I need to write a regular expression that matches a C++ for or while loop that has been terminated with a semi-colon (;). For example, it should match this: for (int i = 0; i < 10; i++); ... but not this: for (int i = 0; i < 10; i++) This looks trivial at first glance, until you realise that the text betwe...

Performance of creating a C++ std::string from an input iterator.

I'm doing something really simple: slurping an entire text file from disk into a std::string. My current code basically does this: std::ifstream f(filename); return std::string(std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>()); It's very unlikely that this will ever have any kind of performance impact on the program...