The question is pretty simple .Currently i am working on a project which have a class lets call it ResoureceHandler.cpp . This class has some properties , method which is needed by the remaining classes . some properties like username, userid which i can get , set simply by calling resourceHandler->getUserName() or vice versa . I can think of two ways
Method 1: Make the class singleton and access the method to get , set.
Method 2 make the class and properties static and access them without any instance .
But i am not sure either of them fall into proper design . What should be the ideal way to solve this kind problem ?
Well, if you want something high performance. Use either of the two methods.
But if you want good coding practice like other languages. You can try something like this:
Why all this?
If there was a lengthy user-related operation, and you suddenly mutate the currentUser. Being a shared_ptr this makes it still unreleased and unmutated INSIDE THE OPERATION, while the static variable will refer to the new one. Also an alternative approach is using copying, but in C++ it may cause some performance problem.