c++

Is there any standard delete functor?

I am looking for a functor that deletes its argument: template<class T> struct delete_functor { void operator()(T* p) { delete p; } }; Is there something like this in std, tr1 or boost? ...

Opt-out of copy constructor

This might be a silly question, but... I've been writing a number of classes that utilize non-copyable members. These classes are never initialized via the copy constructor in my source. When I try to compile without supplying my own copy-constructor, g++ throws out many errors about how it can't build a default copy constructor, due ...

Is there a way to cause a new C++ class instance to fail, if certain conditions in the contructor are not met?

As I understand it, when a new class is instantiated in C++, a pointer to the new class is returned, or NULL, if there is insufficient memory. I am writing a class that initializes a linked list in the constructor. If there is an error while initializing the list, I would like the class instantiator to return NULL. For example: MyCla...

Drawing continuously in drawing application

I was wondering how drawing applications draw the entire time the mouse is down without having empty gaps. What I mean is, for example if the program only drew circles at the mouse's X, y coordinate, then if the mouse went too quicly it would seem like a bunch of little circles rather than a nice continuous line. How can this be done wit...

Is it a good practice to pass struct object as parameter to a function in c++?

I tried an example live below: typedef struct point { int x; int y; } point; void cp(point p) { cout<<p.x<<endl; cout<<p.y<<endl; } int main() { point p1; p1.x=1; p1.y=2; cp(p1); } The result thats printed out is: 1 2 which is what I expected. My que...

C++ new memory allocation fragmentation

I was trying to look at the behavior of the new allocator and why it doesn't place data contiguously. My code: struct ci { char c; int i; } template <typename T> void memTest() { T * pLast = new T(); for(int i = 0; i < 20; ++i) { T * pNew = new T(); cout << (pNew - pLast) << " "; pLast = pNe...

dynamic programming: speeding up this function

i have this program //h is our N static int g=0; int fun(int h){ if(h<=0){ g++; return g; } return g+fun(h-1)+fun(h-4); } is it possible to speed it up using dynamic programming i fugured out this function runs in O(2^n) it means that i suppose to redu...

Can I assume (bool)true == (int)1 for any C++ compiler ?

Can I assume (bool)true == (int)1 for any C++ compiler ? ...

C++ vector reference parameter

Hello folks, let's say we have a class class MyClass { vector<vector<int > > myMatrice; public : MyClass(vector<vector<int > > &); } MyClass::MyClass(vector<vector<int > > & m) { myMatrice = m; } During the instanciation of MyClass, I pass a big vector < vector < int > > and I find that the object is actually copied and not ...

Is there a way to make C macros keyword agnostic?

Is there a way to concatenate keywords in a macro and get C to behave in a more dynamic fashion as in: #define macro(fun,ction,var,iable) function(variable) I know this kind of thing exists in other languages. ...

Creating a C++ static lib to use with Java on Android

I have some C++ code that I want to make into a static lib for use with Java on the Android platform. Can anyone point me to a resource that tells me how to do this? I am completely new to Java and Android. ...

When should I use temporary variables?

Specifically, I'm wondering which of these I should write: { shared_ptr<GuiContextMenu> subMenu = items[j].subMenu.lock(); if (subMenu) subMenu->setVisible(false); } or: { if (items[j].subMenu.lock()) items[j].subMenu.lock()->setVisible(false); } I am not required to follow any style guidelines. After o...

How can a class's memory-allocated address be determined from within the contructor?

Is it possible to get the memory-allocated address of a newly instantiated class from within that class's constructor during execution of said constructor? I am developing a linked list wherein multiple classes have multiple pointers to like classes. Each time a new class instantiates, it needs to check its parent's list to make sure i...

Problem with GetDefaultPrinter() over a VPN c++

I am writing an application that gets its dimensions from the paper ratio of the default printer of the local computer. This all works well unless the default printer is over a VPN. When this is the case, calling GetDefaultPrinter() takes too long. I need to find a way to put a time constraint on GetDefaultPrinter() and if it exceeds tha...

Can my loop be optimized any more? (C++)

Below is my innermost loop that's run several thousand times, with input sizes of 20 - 1000 or more. This piece of code takes up 99 - 99.5% of execution time. Is there anything I can do to help squeeze any more performance out of this? I'm not looking to move this code to something like using tree codes (Barnes-Hut), but towards optimi...

problem using getline with a unicode file

UPDATE: Thank you to @Potatoswatter and @Jonathan Leffler for comments - rather embarrassingly I was caught out by the debugger tool tip not showing the value of a wstring correctly - however it still isn't quite working for me and I have updated the question below: If I have a small multibyte file I want to read into a string I use the...

Lock-Free Data Structures in C++ Compare and Swap Routine

In this paper: Lock-Free Data Structures (pdf) the following "Compare and Swap" fundamental is shown: template <class T> bool CAS(T* addr, T exp, T val) { if (*addr == exp) { *addr = val; return true; } return false; } And then says The entire procedure is atomic But how is that so? Is it not possible that some ...

C++ arrays as parameters, EDIT: now includes variable scoping

Alright, I'm guessing this is an easy question, so I'll take the knocks, but I'm not finding what I need on google or SO. I'd like to create an array in one place, and populate it inside a different function. I define a function: void someFunction(double results[]) { for (int i = 0; i<100; ++i) { for (int n = 0; n<16; +...

What does '#if _LFS64_LARGEFILE-0' mean to CPP?

What does '#if _LFS64_LARGEFILE-0' mean to the C Preprocessor for g++? Is that a minus zero or is that part of the symbol? If it is minus zero, how does that affect whether the #if is triggered? ...

Link Error : xxx is already defined in *****.LIB :: What exactly is wrong?

Problem: I'm trying to use a library named DCMTK which used some other external libraries ( zlib, libtiff, libpng, libxml2, libiconv ). I've downloaded these external libraries (*.LIB & *.h files ) from the same website. Now, when I compile the DCMTK library I'm getting link errors (793 errors) like this: Error 2 error LNK2005: __e...