Singleton vs Static.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Quantum

    #1

    Singleton vs Static.

    Hi,

    What is the difference between a singleton class and a static class?
    From what I can tell, they both offer the same functionality, i.e. only
    one copy of a class.

    Thanks,
    Q
  • Kirit Sælensminde

    #2
    Re: Singleton vs Static.


    Quantum wrote:
    Hi,
    >
    What is the difference between a singleton class and a static class?
    From what I can tell, they both offer the same functionality, i.e. only
    one copy of a class.
    >
    Thanks,
    Q
    You can have many statics of the the same, but only one singleton:

    class A {
    //...
    };
    class B {
    static A a1;
    static A a2;
    static A a3;
    static B &singleton() ;
    };

    There isn't really any such thing as a static class, there are static
    members - either attributes or methods.

    Have a think about the static members of type A in B and then have a
    think about the singleton B. You'll see that they don't offer the same
    functionality in this example, although it is certainly possible to use
    them in such a way that they can.


    K

    Comment

    • Earl Purple

      #3
      Re: Singleton vs Static.


      Quantum wrote:
      Hi,
      >
      What is the difference between a singleton class and a static class?
      From what I can tell, they both offer the same functionality, i.e. only
      one copy of a class.
      A class with every member static is really a collection of functions
      scoped by a class. Although you can use a namespace to do the same, the
      purpose is usually to use the class as a template parameter (something
      you can't do with a namespace).

      std::char_trait s (template) is an example of such a class.

      Comment

      • Quantum

        #4
        Re: Singleton vs Static.

        Earl Purple wrote:
        Quantum wrote:
        >Hi,
        >>
        >What is the difference between a singleton class and a static class?
        > From what I can tell, they both offer the same functionality, i.e. only
        >one copy of a class.
        >
        A class with every member static is really a collection of functions
        scoped by a class. Although you can use a namespace to do the same, the
        purpose is usually to use the class as a template parameter (something
        you can't do with a namespace).
        >
        std::char_trait s (template) is an example of such a class.
        >
        Thank you both for your time. That's quite helpful. :)

        Q

        Comment

        Working...