C + + static-classes can be used without instantiation? __c++

Source: Internet
Author: User
Tags ming

In general, when a class is needed, it must be instantiated before it can be invoked. In the process of programming, it is found that some classes can be used directly without instantiation, using its fields, methods, and so on.

At this time, depend on is static function. static English meaning "static, static", in OOP can be used as modifiers, classes, fields, properties, methods, and so on by static modification, static class, static field, static property, static method ...

The static-decorated class becomes a static class, which can contain only static members (fields, properties, methods) that are statically decorated, cannot be instantiated, cannot be inherited, and non-static can contain static members.


1, when called must first instantiate the situation:

The invoked member is a non-static member (at which point the class it belongs to must be a non-static class). As the following small example:

[CSharp] View Plain Copy Public class classa      //classa class (Non-static Class)    {        public classa ()  { }  //constructor         public void fun () method in  { }//classa Class (Non-static method)   }       public class classb     //needs to invoke the ClassB class    {  of the methods in the ClassA class       PUBLIC CLASSB ()  { }  //constructor         public void fun ()          {            classa a = new classa ()//calling methods in the ClassA class needs to be instantiated first            a.fun ();       }   }         NOTE: The ClassA class is non-static, where the Method fun () is also a non-static method, so calling in CLASSB requires instantiating the ClassA class first.


2, the call does not need to instantiate the situation:

The called member is a static member (at which point it belongs to a static or nonstatic class). As the following small example:

(1) The called class is not a static class:

[CSharp] view plain copy public class classa      // ClassA Class (Non-static Class)    {       public classa ()  { }  // Constructors        public static void fun ()  { }// Methods in the ClassA class (static method)   }      public class classb      //needs to invoke the ClassB class    {       public classb ()  {of methods in the ClassA class  }  //Constructors        public void fun ()           {           classa.fun ();   //calls the method in the ClassA class directly: Class name. Member        }  }           Description: The ClassA class is a non-static class, but the method fun () is a non-static method, so when invoked in ClassB, the ClassA class (and cannot be instantiated) is called without instantiating, calling its members directly, with the syntax "class name. Member".  

(2) The called Class is a static class:

[CSharp]View plain copy public static class ClassA//classa class (Static Class) {//static class cannot have constructors public static void Fun () {       Method in}//classa Class (static method)} public class CLASSB//need to invoke the ClassB class {public classb () {}/constructor of a method in the ClassA class  public void Fun () {classa.fun (); Call the method in the ClassA class directly: the class name. Member}} Description: The ClassA class is a static class, where the method is also fun () as a non-static method, so the ClassA class (and cannot be instantiated) is invoked in ClassB, and the member is called directly. The syntax is "class name. Member".

3. Static modifier (Extended):

(1) A member of a class or class that can be accessed (and not instantiated) without creating an instance, automatically instantiated at the time of invocation, and an instance in memory. When non-static classes that contain static members instantiate objects, they are shared by these static members and can be accessed through class names or object names.
See two interesting little examples on the Web:
A person is a non-static class. People have brains , this is a property decorated with static.
This attribute is for all people and is a common feature of people. Rather than something peculiar to a particular object (for example, John has a brain, Dick has no brains), because as long as it is a person, he has a brain (although Carlos Rodriguez only half of the brain, and some people compare the brain residual →_→, which is also a brain), since it is common to humans, then he can not be instantiated.


Another example that can be instantiated, or the class of man, the height of a person is a non static attribute. Because everyone's height is different. For example, I am a burly, up to 1.55 meters, this 1.55 meters is to describe my height, is related to my specific object. Yao Ming 2.26 meters, this is the data of this object of Yao Ming. Whether it's 1.55 or 2.26, it's all connected to a particular object, not a trait common to humans. So the non static can be instantiated, and static cannot be instantiated.



(2) static variables inside the cosmetic method :
Our code is loaded into memory from the hard drive in order to run, in memory mainly divided into three areas to store our code, respectively, the heap, stack and static storage area. In the heap is the reference type variable in the code, such as the class instance, the array and so on; the stack holds the value types in the code, such as Integer, floating-point, Boolean, and static storage where static variables and global variables, illuminated steady, are stored.

When the whole program is running, the code is a static variable in the common static store, such as the definition of the balance of deposits as static variables, the same bank card number, whether you are in the bank to withdraw money, the ATM machine to take money, or net silver consumption, with this static balance.

So, the static variables inside the method, the execution of the static variable value does not disappear, the method of executing this object again, the value still exists, it is not allocated in the heap and stack, is in the static region is not matched, which is the largest difference with local variables. For memory allocation issues, refer to "Static storage, heap, and stack differences."


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.