Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of this pointer, static member and constant member function in C++

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "C++ this pointers, static members, constant member function example analysis", the content is simple, well-organized, I hope it can help you solve your doubts, now let the editor lead you to study and learn about "C++ this pointers, static members, constant member function example analysis" of this article.

I. this pointer

1. The translation from C++ program to C program

Class CCar {struct CCar {public: int price; int price;}; void SetPrice (int p); void SetPrice (struct CCar * this,int p) {}; this- > price = p Void CCar::SetPrice (int p) {} price = p; / / this- > price = p;} int main () {int main () {struct CCar car; CCar car; SetPrice (& car,20000); car.SetPrice (20000); return 0 Return 0;}}

2. This pointer function: this can be used directly in a non-static member function to represent a pointer to the object that the function acts on.

3. This pointers and static member functions: static member functions do not specifically function with an object, so this pointers cannot be used in static member functions

II. Static members

Static member: a member whose static keyword is added before the description.

Ordinary member variables have their own copy of each object, while static member variables have a total of one, which is shared by all objects, and the sizeof operator does not evaluate static member variables.

Ordinary member functions must act specifically on an object, while static member functions do not act specifically on an object and can be accessed without going through the object.

Class CRectangle {private: int w, h; static int nTotalArea; / / static member variable public: CRectangle (int wicked recording int h _); ~ CRectangle (); static void PrintTotal (); / / static member function}

1. The method of accessing static members:

Class name:: member name CRectangle::PrintTotal ()

Object name. Member name CRectangle r; r.PrintTotal ()

Pointer-> member name CRectangle * p = & r; p-> PrintTotal ()

Quote. Member name CRectangle & ref = r; int n = ref.nTotalNumber

2. Points for attention:

Static member variables are essentially global variables. Even if an object does not exist, static member variables of a class also exist.

Static member variables must be described or initialized once in the file that defines the class. Otherwise, the compilation will pass and the link will not pass.

In static member functions, you cannot access non-static member variables or call non-static member functions

III. Member objects and closed classes

1. Definition: a class with member objects is called an enclosing class

Class CTyre {/ / tyre class private: int radius; / / radius int width; / / width public: CTyre (int r.int w): radius (r), width (w) {}}; class CEngine {/ / engine class}; class CCar {/ / Automotive class private: int price / / Price CTyre tyre; CEngine engine; public: CCar (int pjinint tr,int tw);}; CCar::CCar (int ppenint tr,int w): price (p), tyre (tr, w) {}; int main () {CCar car (20000 minint 17225); return 0;}

In the above example, if the CCar class does not define a constructor, the following statement will make a compilation error: CCar car; because the compiler does not understand how car.tyre should be initialized. Initialization of car.engine is fine, just use the default constructor. Any statement that generates a closed class object should let the compiler understand how the member objects in the object are initialized. The specific way to do this is to close the initialization list of the constructor of the class.

2. The execution order of closed class constructors and destructors

When a closed class object is generated, the constructor of all object members is executed before the constructor of the closed class is executed.

The constructor call order of object members is consistent with the description order of object members in the class, regardless of the order in which they appear in the member initialization list.

When the object of the closed class dies, the destructor of the closed class is executed first, and then the destructor of the member object is executed. The order is opposite to the order in which the constructor is called.

Class CTyre {public: CTyre () {cout

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report