Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

c++ - static class real life significance

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

public class A {
  public class B {
  }

  public static void main(String[] args)
  {
    A.B b1 = new A.B();  // <-- This is ill-formed, because A.B is not
                         //     an independent data type

    A a = new A();
    A.B b2 = a.new B();  // <-- This is correct. Use an instance of A to
                         //     create an object of type A.B.
  }
}

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 type A.B. You cannot do this because B is a member class of A and therefore requires an existing object of type A 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 type A) you must make B a static member class of A:

public class A {
  public static class B {   // <---- I inserted 'static'
  }

  public static void main(String[] args)
  {
    A.B b1 = new A.B();  // <-- This is now well-formed

    A a = new A();
    A.B b2 = a.new B();  // <-- This is now ill-formed.
  }
}

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):

class A
{
public:
  class B
  {
  };
};

int main()
{
  A::B b;   // <--- Perfectly well-formed instantiation of A::B
  return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...