开发者

How can I make an alias to a singleton function?

开发者 https://www.devze.com 2023-01-14 17:34 出处:网络
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 开发者_C百科code.Use

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 开发者_C百科code.


Use a static function.

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

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


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消