In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces C++ function pointers, object pointers, this pointers, pointing to the class static how to use the relevant knowledge, detailed and easy to understand, simple and fast operation, with a certain reference value, I believe that you read this C++ function pointers, object pointers, this pointers, pointing to the class static how to use the article will have a harvest, let's take a look at it.
1. A pointer to a function
The first address of the function code in memory is represented by the function name, that is, the function name is equivalent to the first address of the function code. Therefore, you can define a pointer to a function, that is, a function pointer. The syntax for function pointer definition and assignment is as follows, where the data type represents the return type pointing to the function, and the formal parameter table is the formal parameter table pointing to the function. When assigning values, you must ensure that the return types and parameters of the function name and function pointer are exactly the same:
Data type (* function pointer name) (formal parameter table)
Function pointer name = function name
The following example defines a function and a function pointer that calls the function code through the function pointer:
/ 1. Pre-define a function int addOne (int x) {x = x + 1; return x;} / / 2 that returns int type; define and assign function pointer int (* pointerAddOne) (int x); pointerAddOne = addOne;/// 3, use function pointer in the same way as function name data = pointerAddOne (data); 2. Object pointer
As the name implies, an object pointer represents a pointer to an object. The object name is the address of the object, so you can assign the address of the object to a pointer of the same type, through which the object can be used.
Defining and using object pointers involves four steps:
Define XXX type pointers
Define XXX type objects
Assign an object address to a pointer
Use "(* object pointer name). Member name" or "object pointer name-> member name" use the members of the object
The following example shows the definition and use of object pointers:
Duck duck; / / define and assign object pointers Duck * pointerDuck;pointerDuck = & duck;/// object pointers access members of the object printf ("% d\ n", (* pointerDuck). GetAge ()) in two ways; printf ("% d\ n", pointerDuck- > getAge ()); 3. This pointer
In C++, the data members of each object of the class need to allocate memory separately, but the function members of all objects of the class share memory. The this pointer is the implicit parameter of the non-static member function of the object, which does not need to be defined by yourself. The this pointer points to the object that currently calls the non-static member function. When a class object calls a non-static member function, the address of the object is used as the value of the this pointer, and the non-static member function accesses the data members of the object through the address pointed to by this (the data members of different objects of the class are stored at different addresses, and the this pointer is used to pass the address of the object).
The syntax for the this pointer to access the object data member it points to is:
This- > data member name
In the following example, the member function getAge of the Duck class needs to access the object's data member duckAge, but because a duckAge variable with the same name already exists in the function, you need to access the data member duckAge through this:
Class Duck {public: Duck (int age) {duckAge = age;}; int getAge () {int duckAge = 3; / / access the object's data member duckAge through this instead of the local variable duckAge return this- > duckAge;}; private: int duckAge;}; 4. Pointers to non-static members of the class
First of all, static members and non-static members of a class are different. Static members belong to the class, while non-static members belong to objects. Pointers to non-static members of a class, including pointers to data members and pointers to function members. When declaring, you need to specify the "class name" and "type" that the pointer points to, which represents the data type of the data member or function member:
Type class name:: * data member pointer name
Type (class name:: * function member pointer name) (parameter table)
The Duck class defined below contains public members: int type data member duckWeight and int type function member getAge () (pointers to non-static members must also comply with access permissions and cannot point to private members), respectively, declare pointers to both:
Int Duck::*pointerDuckWeight;int (Duck::*pointerGetAge) ()
The data pointer and function pointer assignment syntax for non-static members is as follows:
Data member pointer name = & class name:: data member name
Function member pointer name = & class name:: function member name
Here are two pointers assigned:
PointerDuckWeight = & Duck::duckWeight;pointerGetAge = & Duck::getAge
The declaration and assignment above are for the class, so it does not point to the member address of the object. This involves the class definition process, when the class definition does not allocate memory, but only determines the memory size and relative location of each data member. Therefore, the data member can be accessed using the starting address of the object plus the relative position. The syntax for non-static data member pointers to access members is as follows:
Object name. * pointer name of the data member
Object pointer name-> * data member pointer name
/ / call example printf ("% d\ n", duck.*pointerDuckWeight)
Function members do not have a copy for each object, but are shared. Object calls function members through this pointers. Calls to non-static member function pointers include the following two syntax:
(object name. Function member pointer name) (argument table)
(object pointer name-> * function member pointer name) (argument table)
/ / the example of the actual call printf ("% d\ n", (duck.*pointerGetAge) ()); 5. Pointer to static members of the class
For static data members and function members of a class, because they do not belong to specific objects, only ordinary data pointers and function pointers are needed.
The Duck class defined below contains static data members and static function members declared with static:
Class Duck {public: static int getAge () {return 666;}; static int duckWeight;}; int Duck::duckWeight = 333
The following is the process of defining and assigning data and functional pointers to static members of a class:
Int * pointerDuckWeight;int (* pointerGetAge) (); / / assignment syntax: pointer name = & class name: static member name pointerDuckWeight = & Duck::duckWeight;pointerGetAge = & Duck::getAge
When calling pointers, you only need to follow the calling syntax of basic data pointers and functional pointers:
Printf ("% d\ n", * pointerDuckWeight); printf ("% d\ n", (* pointerGetAge) ()); about "C++ function pointer, object pointer, this pointer, point to class static how to use" this article is introduced here, thank you for reading! I believe you have a certain understanding of "C++ function pointers, object pointers, this pointers, pointing to class static how to use" knowledge, if you want to learn more knowledge, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.