Hello.
I'm trying to create some kind of error system but I fail and I'm asking if one of you could help me. Here's the deal:
When one of my functions is about to return FALSE (or -1) I'll make it call a SetError function first (with a string as paramater).
When the programs notice that the function returns FALSE it will then call the GetError function and display the error to the user.
My question: How could I create this without using a global variable?

Here is a simple code example where I hope you see what I mean:

Code:

//In "Error.hpp"
# include <windows.h>

LPSTR StrGlobalError = NULL;//This is what I'm trying to avoid.

VOID SetErrorString ( LPSTR StrError )
{

StrGlobalError = StrError;
}

LPSTR StrGetErrorString()//Using a global variable like this makes this function useless
{

return StrGlobalError;
}

Thanks, Ktulu.

05-24-2007

SMurf

What's the problem with it being global exactly?

If you think about how the Win32 error functions work, an integer describing the error is held within memory and retrieved upon request. Therefore, it is a global. The strings aren't, they're pulled out of a resource file and (maybe/maybe not) destroyed after being copied to an application-supplied buffer.

05-24-2007

dwks

I would either use a static global variable and put your error-handling code in a separate source file, or use something like this hack: