static char* FunctionName()

Started by
3 comments, last by Daos 19 years, 8 months ago
I've never seen this syntax before. Can anyone let me in on what is going on here? Snip from FMod library error function:

static char *FMOD_ErrorString(int errcode)
{
    switch (errcode)
    {
        case FMOD_ERR_NONE:             return "No errors";
        case FMOD_ERR_BUSY:             return "Cannot call this command after FSOUND_Init.  Call FSOUND_Close first.";
        case FMOD_ERR_UNINITIALIZED:    return "This command failed because FSOUND_Init was not called";
        case FMOD_ERR_PLAY:             return "Playing the sound failed.";
        case FMOD_ERR_INIT:             return "Error initializing output device.";
        case FMOD_ERR_ALLOCATED:        return "The output device is already in use and cannot be reused.";
        case FMOD_ERR_OUTPUT_FORMAT:    return "Soundcard does not support the features needed for this soundsystem (16bit stereo output)";
        case FMOD_ERR_COOPERATIVELEVEL: return "Error setting cooperative level for hardware.";
        case FMOD_ERR_CREATEBUFFER:     return "Error creating hardware sound buffer.";
        case FMOD_ERR_FILE_NOTFOUND:    return "File not found";
        case FMOD_ERR_FILE_FORMAT:      return "Unknown file format";
        case FMOD_ERR_FILE_BAD:         return "Error loading file";
        case FMOD_ERR_MEMORY:           return "Not enough memory ";
        case FMOD_ERR_VERSION:          return "The version number of this file format is not supported";
        case FMOD_ERR_INVALID_PARAM:    return "An invalid parameter was passed to this function";
        case FMOD_ERR_NO_EAX:           return "Tried to use an EAX command on a non EAX enabled channel or output.";
        case FMOD_ERR_CHANNEL_ALLOC:    return "Failed to allocate a new channel";
        case FMOD_ERR_RECORD:           return "Recording not supported on this device";
        case FMOD_ERR_MEDIAPLAYER:      return "Required Mediaplayer codec is not installed";

        default :                       return "Unknown error";
    };
};


Is the static keyword making the strings global? Or just letting the compiler know that the returning string is not in memory? Or does this function become sort of an advanced macro? Thanks for help.
Advertisement
The static keyword at file scope makes the function (or variable) local to the current translation unit (other cpp files in your program won't see it). It is deprecated in C++ (use anonymous namespaces instead).


String literals such as "No errors" are embedded in the program's binary. "No errors" evaluates to a pointer to the location of that string in the binary.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by Fruny
The static keyword at file scope makes the function (or variable) local to the current translation unit (other cpp files in your program won't see it). It is deprecated in C++ (use anonymous namespaces instead).


String literals such as "No errors" are embedded in the program's binary. "No errors" evaluates to a pointer to the location of that string in the binary.

In this case, the strings are declared in fmod_errors.h.

I'm aware of the use of static in creating global-type variables for function-scope, but I've never seen it used on a return value. I'm still a little unsure of what is happening, why the function needs to use the static keyword, and how it is limited to any scope at all because of the static keyword.

Do you mean that the return value must be shown as static because the string it is returning is only local to the FMOD_ErrorString function scope? What happens if that static char pointer gets passed around in the program? If it truely is a pointer to the exe file memory, then it should be totally safe to store that address value and keep using it?

Thanks for the help!
The static keyword isn't being applied to the return value. It's a linkage class specifier applied to the function. It's like how virtual goes in front of virtual functions declared in classes. For example if you had virtual int function(void), you wouldn't have a virtual int return value, you have a virtual function that returns int. In this case you have a static function that returns char *.
The function is seen only in its cpp file you could do it another way though ,static is kind of an old behaviour use anonymous namespaces
***
namespace{
.....function declaration....
}

This topic is closed to new replies.

Advertisement