Step-by-step study of the properties of the member functions of C + + (class)

Source: Internet
Author: User

A function that is described in the class body is called a member function as a member of a class. A general member function, which is defined according to the needs of a certain kind of function. In addition, some special member functions are discussed: constructors, destructors, copy initialization constructors, and so on. This section discusses some special properties other than the definition and description of member functions. A. inline functions and outer-union functionsthe member functions of a class can be divided into inline functions and external functions. An inline function is a member function defined in the body of a class in which the definition of the function is placed within the body of the class. The description of the member function is placed in the body, and the definition of its function is called an outer function in vitro. If you turn the outer function into an inline function, you only need to add the keyword inline to the left end of the function head. The inline function does not take place in the execution of the program at the time of invocation, but replaces it with the code of the inline function body at the call of the inline function to save the call overhead and improve the efficiency of the operation.
Description: A function is a more advanced abstraction. The introduction of this method makes the programmer only concerned with functions and methods of function, and does not care about the concrete implementation of function function, and the introduction of functions can reduce the program's target code and realize the sharing of program code and data. However, function calls can also be inefficient, because the calling function actually transfers the program execution order to an address in memory where the function is stored, executes the program content of the function, and then returns to where it was before the function was transferred. This transfer operation requires that the site be protected before the transfer and memory of the execution address, back to the scene, and then continue to the original save address. Therefore, function calls have a certain amount of time and space overhead, which will affect their efficiency. In particular, some function body code is not very large, but frequently called function, it is more important to solve its efficiency problem. The introduction of inline functions is actually intended to solve this problem.
At the time of program compilation, the compiler replaces the call expression for an inline function that appears in the program with the function body of an inline function. Obviously, this kind of practice does not produce the problem of turning back, but because the code in the function Hugh is replaced in the program at compile time, it will increase the target program code and increase the space cost, and the time consignment is not as large as the function call, it can be seen that it is at the expense of the increase of the target code to save time.
In a program, when its function is called, the function is replaced at compile time, rather than as a generalfunction is called at run time.

things to be aware of when using inline functions
an inline function has the characteristics of a general function, which differs from a general function in that it is handled by a function call. When a general function is called, the execution of the program is transferred to the called function and then returned to the function that called it, while the inline function is called to replace the call expression with an inline function body. When using inline functions, you should be aware of the following points:

1. A function can self-invoke itself, called a recursive call (referred to later), a function containing a recursive call cannot be set to inline;

2. Use of complex Process Control statements: Loop statements and switch statements, cannot be set to inline;

3. Since inline increases the volume, it is recommended that the code within the inline function be short. Preferably no more than 5 lines.

4.inline is only a "request", and in certain cases the compiler ignores the inline keyword, forcing the function to become a normal function. When this occurs, the compiler gives a warning message.

5. Before you invoke an inline function, the function must be declared before or defined as inline, and if it is previously declared as a normal function, but after the calling code is defined as an inline function, the program can be compiled, but the function does not implement inline.

Sum up to a few words

1. Loop statements and switch statements are not allowed within inline functions.
2. The definition of an inline function must appear before the inline function is first called.

As long as the declaration is inline, the compiler does not compile it into a function call, but just like copying the code of the function to the place where it was called, and this is entirely done privately by the compiler, the original access rights and so on are unaffected. This is not the same as the United States: in ensuring that the code is object-oriented and structured without loss of the conditions, the efficiency of the program has not lost.

Class   MyClass  {public  :  inline   int   GetState ();  Private:  int   m_istate;  }     int inline  myclass::getstate ()  {  return   m_istate;  }  

inline functions have another way of writing, which is to write directly in the class , you do not have to use the "inline" keyword at this time.

Class   MyClass  {public  :  int   GetState () {   return   m_istate;   }  Private:  int   m_istate;  
   inline functions are just a compilation mechanism, and the functions declared in the above two forms are only recommended for the compiler to inline, and the compiler is not necessarily inline. As previously mentioned, the cost of function calls is only for small functions can not be ignored, for the weight of the function is negligible, and in most cases, function calls is the human path, is the best solution to the problem. So most compilers do not compile inline functions with loops, recursion, or code comparisons, and some do not even allow declarations to be inline.

two. Static membersA class is a type, not a data object, and the object for each class is a copy of the data member of that class. However, at some point, all objects of a class need to share some data within the scope of the class. A class member that is declared static can be shared within the scope of a class, called a static member. Therefore, the static member is proposed to solve the data sharing problem. http://blog.csdn.net/skyereeee/article/details/8000512

There are three main functions of static:

(1) Local static variables

(2) External static variables/functions

(3) static data member/member functionbecause global variables do not belong to members of the class, their access rights are complete and open, so it is neither secure nor reusable. Static members have the dual properties of the above problems, not only the data can be encapsulated, but also for all objects to be shared. A static data member is a member of a class that is shared by all objects, not a member of an object. Another advantage of static data members is that you can save memory, and for multiple objects, static data members are stored only one place and are shared for all objects.The value of a static data member is the same for each object, but its value can be updated. As long as the value of the static data member is updated once, all objects are guaranteed access to the same value after the update, which improves time efficiency. 2.1 The initialization of static members takes place outside the body of the class, and it is noteworthy that the previous non-keyword static is meant to avoid confusion with general static variables or objects.
Class   MyClass  {public  :  int   GetState () {   return   m_istate;   }  Private:  int   m_istate;  static int a;//definition}; <span style= "color: #ff0000;" ></span>int myclass::a = 0;//initialization

2.2 Static data members are shared by all objects of the class, including objects of the class's derived classes. That is, a derived class object shares a static data member of a base class with a base class object
StaticForClass.cpp: Defines the entry point of the console application. #include "stdafx.h" class   MyClass  {public  :  int   GetState () {   return   m_istate;   }  The static int a;//defines private:  int   m_istate;  }; int myclass::a = 0;//Initialize class Derivedclass:public myclass{}; int _tmain (int argc, _tchar* argv[]) {MyClass a;derivedclass b;printf ("%d\n", A.A); a.a++;p rintf ("%d\n", B.A); GetChar (); return 0;} <span style= "color: #ff0000;" ></span>
third, static member functions The address of the 3.1 static member function can be stored with a normal function pointer, whereas the ordinary member function address needs to be stored with a class member function pointer
StaticForClass.cpp: Defines the entry point of the console application. #include "stdafx.h" class   MyClass  {public  :  static int   GetState (); void print (); static int a;// Define private:  int   m_istate;  }; int myclass::a = 0;//Initialize int myclass::getstate ()//static function call {return A;//return m_ istate;/* error C2597: Illegal reference to non-static member "Myclass::m_istate" */}void MyClass::p rint () {printf ("%d", a);} Class Derivedclass:public myclass{};int _tmain (int argc, _tchar* argv[]) {#if 0MyClass a;derivedclass b;printf ("%d\n", A.A); a.a++;p rintf ("%d\n", B.A), #endifint (*PF1) () = &myclass::getstate;//normal function pointer//int (MyClass::* pf2) () = &myclass :: Getstate;/*error C2440: "Initialize": Cannot convert from "int (__cdecl *) (void)" to "Int (__thiscall MyClass::*) (void)" */void (MyClass::* pf3 ) () = &myclass::p rint;//normal function pointer GetChar (); return 0;}

The 3.2 static member function does not have the this pointer,. A static member function cannot call a non-static member of a class , it cannot return a non-static member, because the class itself can also be called except when the object calls it.
Class   MyClass  {public  :  static int   GetState ();//static function static int a;//definition private:  int   m_istate;  }; int myclass::a = 0;//Initialize int myclass::getstate ()//static function call {return A;//return m_istate;/* error C2597: Non-static member "Myclass::m_ Istate "Illegal Reference */}

For static member functions, you can summarize the following points:
1. The function definition appearing outside the class cannot specify the keyword static;
2. Static members can be accessed from one another, including static member functions that access static data members and access static member functions;3. Static member functions and static data members can be accessed arbitrarily by non-static member functions;
4. static member functions cannot access non-static member functions and non-static data members;
5, because there is no additional cost of this pointer, so the static member function and the global function of the class will be slightly faster than the speed of growth;
6, call the static member function, you can use the member access operator (.) and (a) call a static member function for an object of a class or a pointer to a class object.
7, when all objects of the same class use a quantity, for this common amount, you can use static data member variable, this variable for all objects of the same class to take the same value. Static member variables can only be called by static member functions. Static member functions are also shared by all objects in the same class. Only static member variables and static member functions can be called.



Step-by-step study of the properties of the member functions of C + + (class)

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.