Limit on Objects Declared

Posted on 2005-05-13
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

    No, you cannot limit the declaration to one single instance. The 'usual' way to go about this is using singletons, see e.g. ("Thread Safe Singleton Template and Management - No MFC Required!")
    LVL 1

    Author Comment

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

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
    What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

    731 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now