C + + Basics Five questions (two)

Source: Internet
Author: User

One. The role of static in C + +

1. Hide: When compiling multiple files at the same time, all global variables and functions without static prefixes have global visibility, and the static function acts to hide

2. The variable can be persisted: variables stored in the static data area are initialized at the beginning of the program, and there are two variables stored in the static store: global variables and static variables, but static variables can control the visible range (hidden)

3. Initialize the static variable to 0 by default

Application in 4.c++

(1) The static function cannot be defined as a virtual function

(2) static data member is stored statically, it must be initialized (programmer manually initialized, otherwise the compilation will not error, but link will be error)

(3) static data member definition or description of the previous to add static;

Two. What does the const keyword do?

1.const modifier variable, pointer, reference

The 2.const modifier member function, which indicates that the member function does not modify member variables, can only call constant member functions, and normal objects can call

Three. can static and const modify member functions at the same time?

No, because the static function is class-owned and does not belong to a specific instance, and const is used to decorate the member function, to ensure that member functions cannot modify the parameters in the class, a this pointer is implicitly added to the function to indicate that it belongs to an instance. So they are contradictory.

Four. The use of const and pointers?

1.const int *a; Constant pointer, the content can not be changed, can not be *a=b way to assign value, but a=&b way to re-specify the direction

2.int Const *A; Constant pointer, same as above

3.int *const A; Pointer constant, the address pointed to by the pointer does not change, but the content stored on the address can be changed (*A=B) (yes), but (a=b) (wrong)

Five. Describe the function of virtual function and the principle of bottom-up implementation?

The main answer is the virtual function table and virtual function table pointer function? Virtual functions in C + + use virtual function table and virtual function table pointer, virtual function table is the Address table of a virtual function of a class, used to index the class itself and the virtual function address of the parent class, if the virtual function of the subclass overrides the virtual function of the parent class, the corresponding virtual function is substituted in the virtual function table as the virtual function address The virtual function table pointer exists in each object (usually for efficiency, at the beginning of the object), he points to the address of the virtual function table of the object's class, and in the multi-inheritance environment, there will be multiple virtual function table pointers, pointing to the virtual function tables of different base classes, respectively.

C + + Basics Five questions (two)

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.