c++

Global Object not accessible to other source files C++

I have an ErrorLog class, that is used to write and modify log files. I'd like to write to it before and after major events for debugging purposes and I only want to use one instance of the ErrorLog class for the entire application. I tried declaring an ErrorLog object as a global by placing ErrorLog exe_log; into a header file so it is ...

Why does the compiler not resolve this call to a template function?

Hi, In below program why does the compiler generate an error for the call to the printMax template function and not the call to the printMaxInts function? #include <iostream> template<class A> void printMax(A a,A b) { A c = a>b?a:b; std::cout<<c; } void printMaxInts(int a ,int b) { int c = a>b?a:b; std::cout<<c; } int...

Using Embarcadero Borland

As I'm more and more dissapointed with VS 2010 I'm trying to find some alternative and I was looking at Embarcadero's new edition of C++ env. Is there any point of learning new (not popular I think) product when VS practically dominates market? Thanks. ...

Perfect Forwarding in C++03

If you have this function template<typename T> f(T&); And then try to call it with, let's say an rvalue like f(1); Why isn't T just be deduced to be const int, making the argument a const int& and thus bindable to an rvalue? ...

How to compare 2 volumes and list modified files?

I have 2 hard-disk volumes(one is a backup image of the other), I want to compare the volumes and list all the modified files, so that the user can select the ones he/she wants to roll-back. Currently I'm recursing through the new volume and comparing each file's time-stamps to the old volume's files (if they are int the old volume). Ob...

Draw on DeviceContext from COLORREF[]

Hi, I have a pointer to a COLORREF buffer, something like: COLORREF* buf = new COLORREF[x*y]; A subroutine fills this buffer with color-information. Each COLORREF represents one pixel. Now I want to draw this buffer to a device context. My current approach works, but is pretty slow (== ~200ms per drawing, depending on the size of th...

How to prevent unnecessary memory use in recursive functions

I've just written a recursive function and it dawned on me that all the variables I use within the function will remain allocated in memory until recursion breaks. If I am recursing a large number of times or allocating large amounts of memory for variables not used in the consequent recursive function call, could this lead to alot of w...

String concatenation

Why it is possible to do const string exclam = "!"; const string str = exclam + "Hello" + " world"; And not possible to do this : const string exclam = "!"; const string str = "Hello" + " world" + exclam; I know (although can't understand why) that it is not allowed to do : const string str = "Hello" + " world" + "!"; as it wil...

Is naming variables after their type a bad practice?

I'm programming C++ using the underscore naming style (as opposed to camel case) which is also used by the STL and boost. However, since both types and variables/functions are named all lower case, a member variable declaration as follows will lead to compiler errors (or at least trouble): position position; A member variable named po...

swapping adjacent nodes in a Linked list

I have problems swapping adjacent nodes in a linkedlist. for ex: input : 1->2->3->4->5->null output: 2->1->4->3->5->null bool swapAdjacent(node** head) { //1->2->3->4->null //2->1->4->3->null if(head==NULL) return 0; node* current = *head; *head = (*head)->next ; node* prev = NULL; cout<<"head val "<<(*head)->data <<endl; ...

C++ Single Header File Structure

I want to speed up the build time of my c++ project, and I am wondering if my current structure may cause unnecessary recompilations. I have *.cc and corresponding *.h files, but all my *.cc files include a single header file which is main.h. In main.h, I include everything necessary and extern global variables and declare the function...

Optimizing away a "while(1);" in C++0x

I have heard and read that C++0x allows an compiler to print "Hello" for the following snippet #include <iostream> int main() { while(1) ; std::cout << "Hello" << std::endl; } It apparently has something to do with threads and optimization capabilities. It looks to me that this can surprise many people though. Does someone...

Problem with Inheritance.

This is a fragment from "Exceptional C++" Item 24, Solution, first bullet from the bottom of the page: Never use public inheritance to implement "IS-ALMOST-A." I've seen some programmers, even experienced ones, inherit publicly from a base and implement "most" of the overridden virtual functions in a way that preserved the semanti...

C++ Weird Diamond inheritance issue

I have this A / \ B C \ / D A has a pure virtual function, prototyped as: virtual A* clone(void) const = 0; B and C virtually inherit from A ( class B: public virtual A, class C: public virtual A) B has the virtual function, prototyped as: virtual B* clone(void) const {}; C has the virtual function, protot...

C++ Specify Headers Include Directory in Code

Is there a way to specify include directories in the code, perhaps via a #pragma? I have my project setup as "src/" and "include/" folders. I am trying to compile in Visual Studio 2010, but I don't want to set it up in the project settings. Is there another way to allow it to compile instead of having to specify the include as #includ...

How to write handles for classes that don't have clone() member?

I'm following an example in Accelerated C++ and writing a simple Handle class that will act as a smart pointer. This uses the virtual ctor idiom using a virtual clone() function. So far so good. But what to do when I want to use my Handle for classes that I don't control that don't provide clone()? The method suggested in the book is to...

Have I managed the memory correctly here? (simple C++ stack)

I'm a complete noob with dynamically allocated memory. Will this have a memory leak or any other memory problem? #include <iostream.h> template <class T> class stack { struct node { T value; node* next; }; public: stack() { ...

Calling friend function defined in struct requires forward declaration?

While reading Karlsson's Beyond the C++ Standard the author defined the friend function intrusive_ptr_add_ref in the body of class reference_counted (see pg 36). That function is called automatically using Argument Dependent Lookup at the proper time. Never having seen friend functions defined in the body of a class, I played around an...

Low latency trading systems using C++ in Windows?

It seems that all the major investment banks use C++ in Unix (Linux, Solaris) for their low latency/high frequency server applications. Why is Windows generally not used as a platform for this? Are there technical reasons why Windows can't compete? ...

Are the C formatted I/O functions (printf, sprintf, etc) more popular than IOStream, and if so, why?

I've been looking through a lot of code made by others lately and happened to notice everyone uses "printf" style C functions a lot, but the C++ functions learned in school (cout, specifically) don't seem so popular. Is this a valid observation, and is there a reason for this? Convention? Thanks, R ...