C++ Trying singleton behavior, keep getting 'undefined reference to Class::instance()
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Now, this isn't the only good pattern for this, but it's the safest one that I can use without going through somersaults. For example, if you need to have multiple implementations of a Database interface, you'll need to handle the singleton in a much more complicated fashion. I using the smaller method name to acquire the instance because that part really isn't important, but what you do with it. I give more space for the important stuff and less space for stuff I don't really care about.
Thanks all, to wrap it up, could you guys tell me what exactly the difference is between Database* and Database& both are references no?
Database * is a pointer, which you might end up deleting somehow. It's much harder to delete a reference like Database &. Also, you are communicating to the programmer using this singleton that the instance returned is never null, since references can never be null (actually, they can, but that's another long obtuse discussion about C++ and compiler implementations). Some singletons need construction parameters, so in that case the singleton acquisition would return a pointer since it should return null if the proper initialization hasn't happened yet.
Quote:
Sorry you have misinterpreted that it was intended for the OP, I realise your code was just an example.
Okay, we're cool then ;-). The original code actually has a more fundamental problem than copy construction. getInstance never changes _instance, so each call to it makes a new object.
@xterm: It's a generally bad idea to use names that start with _, it's possible that those are reserved by the compiler (there's complicated rules as to what exactly is and is not reserved and it's easier to avoid the situation)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.