Limit on Objects Declared

Posted on 2005-05-13
Medium Priority
Last Modified: 2010-04-01
Is there a way in c++ to stop more than one instance of a class from being declared within a program?  Ideally I would like to output some #pragma comment to the compiler if this is not the case.
Question by:reidy_boy
LVL 86

Accepted Solution

jkr earned 500 total points
ID: 13996587
No, you cannot limit the declaration to one single instance. The 'usual' way to go about this is using singletons, see e.g. http://www.codeproject.com/cpp/pdesingletontemplatenomfc.asp ("Thread Safe Singleton Template and Management - No MFC Required!")

Author Comment

ID: 13996689
Wow, how do you guys answer these questions so quickly, do you watch them coming in or something?

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question