Thread: how to implement in C++

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    1

    how to implement in C++

    hi ,

    can any body suggest me on how to implement the below in C++, can this be done

    The compiler should allow me to create only one object for a class , when tried to create another object it should throw an error.

    Eg:
    insert
    Code:
          class A{
            int a;
          }
    
    int main()
    {
       A  a;    // when only one object is created there is not error
       A  b;   // when second object is created compiler should give error
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Search the Web for things like "singleton pattern c++".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    #include <assert.h>
    
    class A
    {
    public:
        A()
        {
            assert(m_Instances++ == 0);
        }
    protected:
        static int m_Instances;
    };
    int A::m_Instances = 0;
    This is an alternative version. Some would like to call the singleton pattern the anti-pattern. I'm not sure I like the singleton pattern myself.
    Sadly, the checks are only done at runtime. I don't know of a way to do it at compile time.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Some would like to call the singleton pattern the anti-pattern.
    I've never heard of the Singleton pattern as an anti-pattern. It is in the gang of 4 design patterns book and is a legit pattern. Any pattern can be an anti-pattern if the pattern is used and applied incorrectly.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Wikipedia
    Some consider it an anti-pattern, judging that it is overused, introduces unnecessary limitations in situations where a sole instance of a class is not actually required, and introduces global state into an application.
    Singleton pattern - Wikipedia, the free encyclopedia
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    Sadly, the checks are only done at runtime. I don't know of a way to do it at compile time.
    I think that the check simply cannot be done at compile time because its evaluation depends on the runtime execution.

    That means that your suggestion is invalid. It also means that if it is really appropriate to enforce a singleton, your suggestion is probably worse than implementing the singleton pattern.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't believe I agree. The singleton pattern basically creates a global from which you can access anywhere.
    My pattern simply makes it impossible to create more than one instance. This forces the object you create to obey scope rules. It also avoids overhead for enforcing the rules (it only asserts in debug mode).
    So, they are two different approaches entirely.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    My pattern simply makes it impossible to create more than one instance. This forces the object you create to obey scope rules.
    One problem is that the relevant logic error can only be detected at runtime. Another problem is that it may not be obvious, without checking the documentation, that the object is supposed to be a singleton. Things like good naming and disabling copying can help, but it will still not be as obvious. (Come to think about it, it is also unclear how should the destruction of such an object be handled.)

    Quote Originally Posted by Elysia
    It also avoids overhead for enforcing the rules (it only asserts in debug mode).
    This might actually be a disadvantage: if due to an oversight a path by which an object of this type is created twice is somehow not tested, but is used in production, there can be a situation where the singleton requirement is violated. Throwing an exception would avoid this, but since this is fundamentally a programmer's logic error, that would be an unfortunate last resort when it can be avoided in the first place.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. where to implement these lines
    By danf004 in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2008, 06:30 PM
  2. Replies: 4
    Last Post: 12-20-2007, 07:55 PM
  3. Replies: 2
    Last Post: 05-07-2007, 06:49 PM
  4. Implement "Whats This" using Win32
    By hemanth.balaji in forum Windows Programming
    Replies: 1
    Last Post: 05-29-2005, 06:03 AM
  5. Can't Implement Tabbing
    By Invincible in forum Windows Programming
    Replies: 10
    Last Post: 02-27-2002, 05:09 AM