is it allowed to use a global variable (C-style) that is a C++ object? -



is it allowed to use a global variable (C-style) that is a C++ object? -

i have object apicontroller handles external library. , access (unique) controller different calls of same function (c-style).

therefor, thought making global apicontroller variable in order access same instance throughout different calls of function given context_t *context different @ every call. @ end, programme calls close function every context.

apicontroller *controller = null; void call(context_t *context) /* called different contexts */ { if (controller == null) controller = new apicontroller(); controller->instances_counter++; /* utilize controller */ controller->use_it(); } void close(context_t *context) { controller->instances_counter--; if (controller->instances_counter == 0) delete controller; }

is proper proceed way? sense not don't see easy way of doing it.

it dependes. if api & controller either stateless (so ever need 1 of them), or it's state not stored in apicontroller object (you have no way of creating 2 different api controller) should create apicontroller class singleton.

there one thousand posts out there when not utilize singletons, case 1 of rare case when create sense.

on other hand if can create 2 difference configurations of apicontroller, , may think sometime in future you'd perchance want utilize more 1 of them, should, either pass parameter call or improve create phone call fellow member function of class store information.

class someobject { private: apicontroller * api; public: // ... void call(context_t context) { // ... } // ... };

ultimately makes sense you, maintain in mind you'll need rewrite code couple of times before final version, thinking along lines of "i'll ever need 1 of object" may come on 1 day.

c++ global-variables

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -