c++

Which allocator are available in STLPORT, and how to use them

We're using STLPORT and we'd like to change stlport's default allocator: instead of vector<int>, we'd like to try vector<int, otherallocator> Which alternative allocator are available in stlport, and what are their features? How do I use them? ...

What does this C code do [Duff's device]?

void Send(int * to, const int* from, const int count) { int n = (count+7) / 8; switch(count%8) { case 0: do { *to++ = *from++; case 7: *to++ = *from++; case 6: *to++ = *from++; case 5: *to++ = *from++; case 4: *to++ = *from++; case 3: *to++ = *from++; c...

How to handle different situations in mouse event handler properly?

In my Qt application in the event handler for mouse press events I have such ugly code void Render::Viewer::mousePressEvent(QMouseEvent* e) { switch (e->button()) { case Qt::LeftButton: switch (mode_) { case Render::Viewer::ModeView: switch (e->modifiers()) { case Qt::NoModifier: ... b...

Configuration structs vs setters

I recently came across classes that use a configuration object instead of the usual setter methods for configuration. A small example: class A { int a, b; public: A(const AConfiguration& conf) { a = conf.a; b = conf.b; } }; struct AConfiguration { int a, b; }; The upsides: You can extend your object and easily gua...

how to find file with syntax error in Visual Studio?

Hey there! I guess many of you know this problem, when programming C++ with visual studio, there are cases which a simple syntax error can cause many hundreds of compiler errors. For example if you forget the ; in the end of a class definition, Visual studio will output many syntax errors in files which are actually completely fine. Is...

Interface Inheritance in C++

I have the following class structure: class InterfaceA { virtual void methodA =0; } class ClassA : public InterfaceA { void methodA(); } class InterfaceB : public InterfaceA { virtual void methodB =0; } class ClassAB : public ClassA, public InterfaceB { void methodB(); } Now the following code is not compilable: int...

Insert into an STL queue using std::copy

Hi, I'd like to use std::copy to insert elements into a queue like this: vector<int> v; v.push_back( 1 ); v.push_back( 2 ); queue<int> q; copy( v.begin(), v.end(), insert_iterator< queue<int> >( q, q.front() ) ); But this fails to compile, complaining that 'begin' is not a member of 'std::queue'. Note: I tried it with std::inserter...

Template specialization of a single method from a templated class

Hello. Always considering that the following header, containing my templated class, is included in at least two .CPP files, this code compiles correctly: template <class T> class TClass { public: void doSomething(std::vector<T> * v); }; template <class T> void TClass<T>::doSomething(std::vector<T> * v) { // Do somtehing with a vect...

What is the best solution for suppressing warning from a MS include (C4201 in mmsystem.h)

I am tired of having to look at warnings during our compilations - warnings that come from MS include files. "C:\Program Files\Microsoft SDKs\Windows\v6.0A\include\mmsystem.h(1840): warning C4201: nonstandard extension used : nameless struct/union" I have seen this thread that suggests changing the header itself (but then each of my te...

how to perform bitwise operation on floating point numbers

i tried this: float a = 1.4123; a = a & (1 << 3); i get a compiler error saying that operand to & cannot be of type float. when i do: float a = 1.4123; a = (int)a & (1 << 3); i get the program running. only thing is that the bitwise operation is done on the integer representation of the number obtained after rounding off. float a...

What happens when QueryPerformanceCounter is called?

I'm looking into the exact implications of using QueryPerformanceCounter in our system and am trying to understand it's impact on the application. I can see from running it on my 4-core single cpu machine that it takes around 230ns to run. When I run it on a 24-core 4 cpu xeon it takes around 1.4ms to run. More interestingly on my machin...

How to define abstract base classes for arbitrary (but compile time defined) features support?

I have the following C++ design problem and I would really appreciate any suggestion/solution. Please notice that my background is not in computer science, so I may be missing some obvious solution. The way I usually separate key components in the code is to define interfaces via abstract classes and pure virtual functions. Example1: ...

IOServiceAddMatchingNotification issue

Hello, void functions::start() { io_iterator_t enumerator; ...some code... result = IOServiceAddMatchingNotification( mNotifyPort, kIOMatchedNotification, IOServiceMatching( "IOFireWireLocalNode" ), serv...

Accessing protected member functions from test code in C++

I've been racking my brain trying to think of the best way to access a protected member function from some test code in C++, here's my problem: //in Foo.h Class Foo { protected: void DoSomething(Data data); } //in Blah.h Class Blah { public: Foo foo; Data data; }; //in test code... Blah blah; blah.foo.DoSomething(blah.da...

Why doesn't my custom iterator work with the STL copy?

I wrote an OutputIterator for an answer to another question. Here it is: #include <queue> using namespace std; template< typename T, typename U > class queue_inserter { queue<T, U> &qu; public: queue_inserter(queue<T,U> &q) : qu(q) { } queue_inserter<T,U> operator ++ (int) { return *this; } queue_inserter<T,U> operat...

Using C++ libraries in an R package

What is the best way to make use of a C++ library in R, hopefully preserving the C++ data structures. I'm not at all a C++ user, so I'm not clear on the relative merits of the available approaches. The R-ext manual seems to suggest wrapping every C++ function in C. However, at least four or five other means of incorporating C++ exist...

Splitting templated C++ classes into .hpp/.cpp files - is it possible?

g++ -c -o main.o main.cpp g++ -c -o stack.o stack.cpp g++ -o main main.o stack.o main.o: In function `main': main.cpp:(.text+0xe): undefined reference to 'stack::stack()' main.cpp:(.text+0x1c): undefined reference to 'stack::~stack()' collect2: ld returned 1 exit status make: *** [program] Error 1 stack.hpp: #ifndef _STACK_HPP #defin...

Const correctness for value parameters

I know there are few question about const correctness where it is stated that the declaration of a function and its definition do not need to agree for value parameters. This is because the constness of a value parameter only matters inside the function. This is fine: // header int func(int i); // cpp int func(const int i) { return...

DirectX 9 Terrain Engine Problem C++

Hello, I'm Having a problem with my DirectX 9 Terrain Engine.. It's working fine, except for one thing, it doesn't load the heightmap in a proper way. You can see a screenshot of the problem here: as you can see there is a diagonal crack through the entire map.. one side should be mirrored to render the map properly. I'm almost sure t...

C++ std::string and NULL const char*

I am working in C++ with two large pieces of code, one done in "C style" and one in "C++ style". The C-type code has functions that return const char* and the C++ code has in numerous places things like const char* somecstylefunction(); ... std::string imacppstring = somecstylefunction(); where it is constructing the string from a c...