Singleton vs Static.

Q

Quantum

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
 
?

=?iso-8859-1?q?Kirit_S=E6lensminde?=

Quantum said:
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
 
E

Earl Purple

Quantum said:
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_traits (template) is an example of such a class.
 
Q

Quantum

Earl said:
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_traits (template) is an example of such a class.

Thank you both for your time. That's quite helpful. :)

Q
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,182
Latest member
BettinaPol

Latest Threads

Top