2
votes

I would like to make an alias in C++ to singleton calling so instead of calling MYCLASS::GetInstance()->someFunction(); each time, I could call just someFunctionAlias(); in my code.

4
You could always save yourself a lot of pointlessness by not using a singleton. - Puppy
I updated your question so it talks about aliases instead of typedefs; the typedef keyword is specifically for types. - Donal Fellows

4 Answers

3
votes

Use a static function.

namespace ... {
    void someFunction() {
        MYCLASS::GetInstance()->someFunction();
    }
};

Edit: Sorry lads, I wrote static someFunction and meant void someFunction.

1
votes

typedefs are used for type aliases but can't be used as call alias.

functions (such as suggested as by DeadMG) can be used as a call "alias".

PS. As this is C++ you have lots of options, function pointers, std::tr1::function<> operator overloading and the preprocessor. But in this case it certainly looks like a simple function would be the simplest and best solution.

0
votes

Look up function pointers.

You can create a function pointer, and assign it to your long function. You can then call this function pointer just like a regular function, wherever your variable is defined.

Function pointers can be confusing, but are used a lot in API callbacks (i.e. you pass a function as an argument to the API, and the API will call that function when something happens (think WndProc)).

Good luck.

-1
votes

you can do this #define someFunctionAlias MYCLASS::GetInstance()->someFunction()