Want to use Global Variable to present instance-related value?

Sometimes we may need to use a global variable to present a value acting like a class member variable, but we couldn’t define it into the class header file (Don’t ask why because cross-platform developers may really have this kind of requirements).

The solution is simple: use a static map as the global variable.

For example, in the mm file:

Top:

static map<YourClassName*, bool> isUsingXFeature;

Initial:

if (shouldUseXFeature())
{
   isUsingXFeature[this] = true;
}

Using:

if (isUsingXFeature[this]) 
{
   doSomething();
}

Of course you can define the global variable as unordered_map to go a fancier way.

 

 

Published by

vivo

This is Vivo. Yes, I'm Vivo.

Leave a Reply

Your email address will not be published. Required fields are marked *