5

Is it possible to create a class which could be constructed just one time? If you would try to create an other instance of it, a compile-time error should occure.

4
  • 4
    What you're looking for is called a singleton. en.wikipedia.org/wiki/Singleton_pattern I must urge you though to very seriously consider whether a singleton really makes sense for your application. 99% of the time a singleton is used, it should not be. Commented Apr 28, 2012 at 8:33
  • However, the singleton will just return the same instance for all creation queries. Probably this is even more useful that receiving exception. Commented Apr 28, 2012 at 8:34
  • AFAIK there is no option to make such class with compile time error. But there is a popular pattern called 'Singleton' for making such class that preventing instancing. You can easily google tons of examples. Commented Apr 28, 2012 at 8:35
  • @rafdp: Why do you want this? This smells like an XY Problem. Commented Apr 28, 2012 at 8:46

3 Answers 3

6

Instantiation is dynamic, at run time. Compilation errors are at compile time. So the answer is no, it's not possible to get a compilation error on any second instantiation.

You can however use a singleton, but do consider very carefully whether it's really needed.

Sign up to request clarification or add additional context in comments.

Comments

1

The classes with only one instances are called singleton classess,

There are many ways to perform that. The simplest is shown below

class MySingleton
    {
    public:
      static MySingleton& Instance()
      {
        static MySingleton singleton;
        return singleton;
      }

    // Other non-static member functions
    private:
      MySingleton() {};                                 // Private constructor
      MySingleton(const MySingleton&);                 // Prevent copy-construction
      MySingleton& operator=(const MySingleton&);      // Prevent assignment
    };

6 Comments

rafdp asked for example of compile-time error, this is not an answer.
@Yarg If you will try to create more than one instances of class MySingleton then tis will give compilation error, this is what rafdp asked.
How about static MySingleton& OtherInstance() { static MySingleton anotherSingleton; return anotherSingleton; }
@Yarg If you are saying the class definition could be modified to create another instance, then of course. But I think the OP was talking about a scenario where the class was designed so that USERS of the class would get a compile error. I think it's reasonable to assume these users wouldn't modify the class to get around the restrictions.
@Jaguar: The code is returning the reference. So there is no way to demonstrate compile time error.MySingleton &a = MySingleton::Instance(); MySingleton &b = MySingleton::Instance(); Both a and b will be reference to the single static instance singleton
|
0

Why compile error? You just need to implement Singleton design pattern, I think. Look here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.