The difference between const and static in C + +

Source: Internet
Author: User

  

A const-defined constant is freed after its scope has been exceeded, and static constants defined by Statics do not release their storage space after the function executes.

Static is expressed statically. Static member functions and static member variables of a class are related to the class, not to the specific object of the class. Static member functions and member variables of a class can be called even if there are no specific objects. A static function of a general class is almost a global function, except that it is scoped to the file containing it.

In C + +, static statically member variables cannot be initialized inside a class. Inside a class is just a declaration, the definition must be outside the body of the class definition, usually initialized in the class's implementation file, such as: The double account::rate=2.25;static keyword can only be used in declarations within the class definition body, and cannot be marked as static when defined

In C + +, const member variables cannot be initialized at the class definition, only through constructor initialization lists, and must have constructors.

Const data members are constants only for the lifetime of an object, but are mutable for the entire class. Because a class can create multiple objects, different objects can have different values for their const data members. The const data member cannot be initialized in the declaration of the class, because the compiler does not know what the value of the Const data member is when the object of the class is not created.

The initialization of a const data member can only be done in the initialization list of the class's constructor. To build constants that are constant throughout a class, you should use enumerated constants in the class, or static cosnt.

[CPP]View Plaincopy
  1. Class Test
  2. {
  3. Public
  4. Test (): A (0) {}
  5. enum {size1=100,size2=200};
  6. Private
  7. const int A; Can only be initialized in the constructor initialization list
  8. static int b; Defined and initialized in the implementation file of the class
  9. const static int C;   Same as static const int C;
  10. };
  11. int test::b=0;  The //static member variable cannot be initialized in the constructor initialization list because it does not belong to an object.
  12. cosnt int test::c=0; Note: When assigning a value to a static member variable, you do not need to add the static modifier. But to add cosnt .

The main purpose of the COSNT member function is to prevent member functions from modifying the contents of an object. That is, the const member function cannot modify the value of a member variable, but it can access the member variable. When a method member function, the function can only be a const member function.

The static member function is primarily intended as a global function for the scope of a class. You cannot access non-static data members of a class. The static member function of the class does not have the this pointer, which causes: 1, cannot directly access non-static member variables of the class, call non-static member function 2, cannot be declared as virtual

Initialization problems with static, const, static cosnt, const static members:

1. Const member initialization in class:

When you create a const in a class, you cannot give him an initial value.

[CPP]View Plaincopy
    1. Class Foo
    2. {
    3. Public
    4. Foo (): I (100) {}
    5. Private
    6. const int i=100;   Error!!!
    7. };
    8. Or in such a way as to initialize
    9. Foo::foo (): I (100)
    10. {}

2. Initialization of static members in class:

A static variable in a class belongs to a class, does not belong to an object, it has only one copy during the entire program's run, so the variable cannot be initialized when the object is defined, or it cannot be initialized with a constructor, the correct initialization method is:

Data type class Name:: static data member name = value;

[C-sharp]View Plaincopy
    1. Class Foo
    2. {
    3. Public
    4. Foo ();
    5. Private
    6. static int i;
    7. };
    8. int foo::i=20;
    9. This shows that:
    10. 1. Initialization is performed outside the class body, and the front is not static, so as to avoid confusion with general static variables or objects
    11. 2. Initialization without the access control of the member private, public, etc.
    12. 3. Initialization uses a scope operator to indicate the class to which it belongs, so a static data member is a member of a class rather than a member of an object.

3. Static cosnt and const static member initialization in class

In the same way that these two formulations work, in order to facilitate memory, this value describes a common initialization method:

[CPP]View Plaincopy
    1. Class Test
    2. {
    3. Public
    4. static const int mask1;
    5. const static int mask2;
    6. };
    7. Const TEST::MASK1=0XFFFF;
    8. Const TEST::MASK2=0XFFFF;
    9. There is no difference in their initialization, although one is a static constant and one is constant static. The static is stored in the global variable area, in fact the final result is the same. may be handled differently within different compilers, but the final result is the same.

This is a complete example:

[CPP]View Plaincopy
  1. #ifdef A_h_
  2. #define A_h_
  3. #include <iostream>
  4. Using namespace std;
  5. Class A
  6. {
  7. Public
  8. A (int a);
  9. static void print (); Static member functions
  10. Private
  11. static int aa; Declaration of a static data member
  12. static const int count; Constant static data member (can be initialized in constructors)
  13. const int bb; Constant data members
  14. };
  15. int a::aa=0; //Definition of static member + initialization
  16. const int a::count=25; Static constant member Definition + initialization
  17. A::A (int a): BB (a)//initialization of constant members
  18. {
  19. Aa+=1;
  20. }
  21. void A::p rint ()
  22. {
  23. cout<<"count=" <<count<<endl;
  24. cout<<"Aa=" <<aa<<endl;
  25. }
  26. #endif
  27. void Main ()
  28. {
  29. A (10);
  30. A::p rint (); //Access static member functions by class
  31. A.print (); //accessing static member functions through objects
  32. }

The difference between const and static in C + + (GO)

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.