Static variable in VC

Source: Internet
Author: User

There is no forced conversion between data types in VC. Both Java and C have:

For example, in VC:

Int A = 0 xfffe; // that is, 65534
Char B = A; no compilation Error

Str. Format ("% d", B );
MessageBox (STR); // The result is-2.

In Java:

Int A = 0 xfffe; // that is, 65534
Byte B = A; Compilation error. Change to byte B = (byte)

 

Static variable (static). The reason why the static variable is because it isProgramThe lifecycle Address remains unchanged. That is to say, only one copy is saved in the entire program. The static keyword allows it to keep its value between different function calls. If the value of a static variable changes after it is accessed, it will keep the new value. If this variable is accessed again, it will maintain the latest value.
Class test1 {public: static int A; const int B; test1 (): B (10) {} void print1 () {A ++; printf ("% d, % d \ n ", a, B) ;}; int test1: A = 10; void main () {test1 T1; t1.print1 (); test1 T2; t2.print1 (); test1 T3; t3.print1 ();}

Result: 11 12 13

In C ++, static member variables cannot be initialized within the class. Int test1: A = 10;
In C ++, the const constant member variables cannot be initialized in the class definition. They can only be initialized through constructors.Initialization listAnd must have constructors.Initialization fails in the constructor.

 

Common keywords in C ++:

1. inline: defines an inline function. This keyword is defined-based. If inline is provided only when the function is declared, the function is not considered to be an inline function, therefore, you must add inline to the function definition. At the same time, inline only recommends to the compiler that the function should be processed internally, rather than forcibly.

2. const: defines regular members, including const data members and const member functions. Const data members are required and can only be initialized through the initialization list of const functions. Const member functions can only be members of the category, cannot be modified. If you need to modify it, the following mutable keyword is introduced.

3. mutable: This keyword is introduced to solve the problem that the const member function needs to modify the member variable. Generally, the const member function can only access the member variable but cannot be modified. However, if the member variable is mutable, you can modify the variable in the const member function. Mutable and const cannot be used to modify member variables at the same time.

4. static: Declares static members, including static data members and static member functions, which are shared by all objects of the class. static data members must be initialized before use, the static member function can only access static data members, but cannot access non-static data members because the function does not include the this pointer.

5. Virtual: declares a virtual function to implement polymorphism. This keyword is based on declaration.

6. Friend: declares a friend function and a friend class. This keyword is also based on the declaration.

7. Volatile: The variable modified by this keyword indicates that its value may be modified beyond the scope recognized by the compiler. Therefore, the compiler does not optimize the operations of this variable. You can modify a variable with Const.

 

Why do we need a const member function?

In the member functions of the classes we define, some member functions often access common objects and constants without changing the data members (common members) of the classes. That is to say, these functions are "read-only" functions, and some functions need to modify the values of class data members. If you add the const keyword to all functions that do not change data members for identification, it can obviously improve the readability of the program. In fact, it can also improve the program's reliability. It has been defined as a const member function. Once it attempts to modify the value of the data member, the compiler will handle it by mistake.

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.