I read a lot on static keyword, I only found static variable, static function, but there is no discussion of static class, can you please explain me about this.
- Why we use static class in c++?
- Why we introduce this type of class?
- Give the physical significance of static class?
- Give the real life example of static class?
- If there is any limitation, then tell me what?
I am waiting for your reply. Thanks in advance.
There are no static classes in C++.
static
refers to a storage class, i.e. it applies to objects or functions, not to data types.In Java,
static class
, when applied to classes that are nested in other classes, means that the nested class can be instantiated independently of any instance of the enclosing class. In C++ that is always the case. Nested classes in C++ are always independent data types.Here is what I mean: First let's take a look at this Java code:
It defines a class
A
, and a nested class (i.e. a member class, or sub-class)A.B
. The second line of the main program shows how you cannot instantiate a object of typeA.B
. You cannot do this becauseB
is a member class ofA
and therefore requires an existing object of typeA
to be instantiated. The third line of the main program shows how this is done.In order to be able to instantiate an object of type
A.B
directly (independently of any instance of typeA
) you must makeB
a static member class ofA
:On the other hand, in C++ this is not required, because in C++ a member class is always an independent data type (in the sense that no instance of the enclosing class is required to be able to create instances of the nested class):