c++

Where to put the enum in a cpp program?

I have a program that uses enum types. enum Type{a,b,}; class A { //use Type }; class B { // also use that Type }; 2 class are located in 2 different files. Should I put the type definition in a headfile or in class definition for each class? ...

Should I delete vector<string> ?

I've painfully learned during last few days a lot about programming in c++. I love it :) I know I should release memory - the golden "each malloc=free" or "each new=delete" rules exist now in my world, but I'm using them to rather simple objects. What about vector ? Wherever I can, I'm using vector.clear() but that clearly isn't enough, ...

How can I configure my project to generate platform independent code?

I am writing an application that I would like to release binaries for on Mac, Windows, and Linux. I have code that compiles under Mac and Linux, but under Windows, it does not. This is because of Windows lack of a strcasecmp. I've read a little bit about how I can create some sort of header to wrap my code, but I don't really understand...

Determining Client Internet Connection (InternetGetConnectedState() lies)

It seems I am not the only one to have discovered this: http://forums.devx.com/showthread.php?t=72771 Actually both: InternetGetConnectedState InternetGetConnectedStateEx are not reporting correctly. When I am connected, it returns 0x12 and when I an not connected it returns 0x10 (0x10 is not defined) http://msdn.microsoft.com/en-us/...

how-to do Crossplatform transparent windows in c++?

Hello, I'm woundering how to make a window transparent, not cutout holes or the same transparency overall. Well, just say I wanna slap a PNG-picture of a rose or something and have it blend nicely with stuff in behind and allow stuff behind to redraw and have their changes shine throught the transparent parts of the picture/window. I c...

Weird striping in tile graphics on Nintendo DS

I'm working on a pong game for the Nintendo DS. I'm using libnds to set things up and I've come across a very strange behaviour. So far I've only tried it out in emulators, but I use three different ones and they all exhibit this behaviour so I suspect I'm doing something bad. The actual problem is that when I use background layer 1 or ...

Advice on wrapping third party libraries

I have been working a year now as a software developer for a at the computer-vision department of a company. My main job is integration of third-party software into a framework, so i usually end up writing wrapper libraries because a lot of this third party software does not work the way we want it to work(not thread safe, pain in the a*...

What are the best affordable c++ audio libraries?

I'm looking for a c++ audio library that can play mp3s and mix audio. I can spend some money on it, but I'm on a somewhat tight budget, so the less the better. It really needs to just run on Windows and Mac OS X although Linux and embedded devices are a big plus. Anyone know of a good library or two? Thanks! ...

Two classes that refer to each other

I'm new to C++, so this question may be basic: I have two classes that need to refer to each other. Each is in its own header file, and #include's the other's header file. When I try to compile I get the error "ISO C++ forbids declaration of ‘Foo’ with no type" for one of the classes. If I switch things so the opposite header gets pa...

size_t vs int in C++ and/or C

Why is it that in C++ containers, it returns a size_type rather than an int? If we're creating our own structures, should we also be encouraged to use size_type? ...

Static variable inside template function

In C++, if you define this function in header.hpp void incAndShow() { static int myStaticVar = 0; std::cout << ++myStaticVar << " " << std::endl; } and you include header.hpp in at least two .cpp files. Then you will have multiple definition of incAndShow(). Which is expected. However, if you add a template to the function templa...

Law of Demeter doesn't make sense in my case

Looking on this answer I understand that you should not copy private pointers using friendship in C++ like I did in my program: class bar; class foo { private: some_smart_pointer<int> state; public: foo(int value) : state(new int(value)) {} friend class baz; }; class bar { private: some_weak_pointer<int> state; public: ...

What is Proxy Class in C++

I wanted to know What is Proxy Class in C++, why it is created and where it is useful. Thank a lot in advance. ...

Header file for functions inside of a namespace?

In C++ I have a file A.cpp that has the following in it: namespace Foo { bool Bar() { return true; } } How would I declare this function in A.h? How do I handle the namespace? ...

How to do an integer log2() in C++?

In the C++ standard libraries I found only a floating point log method. Now I use log to find the level of an index in a binary tree ( floor(2log(index)) ). Code (C++): int targetlevel = int(log(index)/log(2)); I am afraid that for some of the edge elements (the elements with value 2^n) log will return n-1.999999999999 instead of n....

C++: Problem with ptr_multimap::insert

Please consider the following code: #include "boost/ptr_container/ptr_map.hpp" int main() { typedef boost::ptr_multimap<char, const int> M; M m; char c = 'c'; int* j = new int(7); m.insert(c, j); return 0; } The gcc 4.3.3 compiler fails to compiler the above code. Am I doing something w...

Rounding doubles - .5 - sprintf

I'm using the following code for rounding to 2dp: sprintf(temp,"%.2f",coef[i]); //coef[i] returns a double It successfully rounds 6.666 to 6.67, but it doesn't work properly when rounding 5.555. It returns 5.55, whereas it should (at least in my opinion) return 5.56. How can I get it to round up when the next digit is 5? i.e. return...

Is there ever a need for a "do {...} while ( )" loop?

Bjarne Stroustrup (C++ creator) once said that he avoids "do/while" loops, and prefers to write the code in terms of a "while" loop instead. [See quote below.] Since hearing this, I have found this to be true. What are your thoughts? Is there an example where a "do/while" is much cleaner and easier to understand than if you used a "wh...

Passing a Safearray of custom types from C++ to C#

Hi, how can one use a Safearray to pass an array of custom types (a class containing only properties) from C++ to C#? Is using the VT_RECORD type the right way to do it? I am trying in the following way, but SafeArrayPutElement returns an error when trying to fill the safearray the reference to the array of classes gets to the managed ...

Specialize Function Templates vs Function Overload vs Class Specializing

According to this article from Herb Sutter, one should always pick Class Specializing over Function Overload and definitely over Specialized Function Templates. The reason is that Specializations don't overload. Overload resolution only selects a base template (or a nontemplate function, if one is available). Only after it's been ...