Shorter calls / names without using definitions

A simple question: how can I shorten the name of the call without using define.

For example, I have a singleton, which I have to call, which is in the namespace (I cannot use it using the blabla namespace , because this is invalid):

MyFW::GameRoot::Instance()->DoSomething();

Now I can assign this to a variable that works several times, if I use it several times within the same class / function, but using it in many classes / functions, it becomes cumbersome. I decided to use #define for it:

#define MyFW::GameRoot::Instance() ROOT //defined in GameRoot.h

ROOT->DoSomething(); //Used where-ever GameRoot.h is included

Much better, and I really like it, especially because now, wherever I am, ROOT (color is encoded via V-Assist), I know that right away ... if I don't have a breakpoint and I need Visual Studio to solve the ROOT is displayed in the clock window (or even hang it on it to quickly pull out the object in debugging), which it cannot do.

Is there any other option? What are you doing to shorten the names? Just use local / member pointers to store the instance?

Thank!

+3
source share
4 answers

If you want to facilitate access to several functions, just use the helper function:

namespace {
    MyFW::GameRoot* root() { return MyFW::GameRoot::Instance(); }
}

// ...
root()->DoSomething();

Two characters are larger, but it comes with security type enabled.

+2
source

:

MyFW::GameRoot& ROOT = *MyFW::GameRoot::Instance();

define.

+5

...,

namespace root=MyFW::GameRoot;

Then you can enter

root::Instance()->DoSomething();

Defining such a namespace is better than #define. (I cannot be mistaken, otherwise the compiler knows what you are trying to do.)

+5
source

Good way to do it (but never in the title)

using MyFW::GameRoot;
GameRoot::Instance()->DoSomething;

This declaration of use differs from the using directive, as you mentioned above.

+1
source

Source: https://habr.com/ru/post/1754933/


All Articles