VC + + Class object-oriented (iv)

Source: Internet
Author: User
Tags gety

This section is about inline functions, why use inline functions?

When the compiler discovers that a piece of code calls an inline function, it does not call the function, but instead inserts the code of the function into the current position. The benefit of this is that the process of calling is omitted and the program runs faster. (the function calls the procedure, because has previously said the parameter into the stack and so on operation, therefore must occupy some time more often). This is not good: Because whenever the code calls into an inline function, you need to insert the code of the function directly at the call, so the volume of the program increases. Take the life phenomenon analogy, like the TV is broken, through the telephone to find the repairman, you will be too slow, so simply at home to keep a repairman. This is, of course, fast, but the repairman is going to have to take the floor when he lives in your house. An inline function is not required, it is just a modification to improve speed. To modify a function to be inline, use the following format:
declaration or definition of an inline function
In a simple sentence, add an inline modifier before the function declaration or definition.

inline int max (int a, int b)
{
Return (A>B)? A:B;
}

The intrinsic nature of inline functions is that it saves time but consumes space.

Ii. rules of the inline function

(1), a function can be self-called self, called a recursive call, a function containing a recursive call cannot be set to inline;

(2), the use of complex process Control statements: Loop statements and switch statements, can not be set to inline;

(3), because inline increases the volume of the characteristics, it is recommended that the code within the inline function should be very short. Preferably no more than 5 lines.

(4), inline only as a "request", in particular cases, the compiler will disregard the inline keyword, and forcing the function to become a normal function. When this occurs, the compiler gives a warning message.

(5), before you call an inline function, this function must be declared before or defined as inline, if the previous declaration as a normal function, and after the calling code is defined as an inline function, the program can be compiled, but the function does not implement inline. For example, the following code snippet:
The function is not declared inline at first:
void Foo ();
Then there is the code that calls it:
Foo ();
Only after the call has the function defined as inline:
inline void foo ()
{
......
}
The code is the Foo () function does not finally implement inline;

(6), for debugging convenience, when the program is in the debugging phase, all inline functions are not implemented.

Third, the use of inline functions should pay attention to the following issues:

(1) inline functions defined in one file cannot be used in another file. They are usually shared in the header file.
(2) The inline function should be concise, only a few statements, if the statement is more, not suitable for the definition of inline functions.
(3) in the inline function body, there cannot be a loop statement, an if statement, or a switch statement, otherwise the compiler will treat the function as a non-inline function, even if there is an inline keyword.
(4) The inline function is declared before the function is called. The keyword inline must be placed with the function definition body in order for the function to be inline, and only the inline is placed in front of the function declaration with no effect

Implementation of the inline function (i)

File2.hclass point{public:point (int x,int y); ~point (); int GetX () Const{return m_x;} int GetY () Const{return m_y;} void SetX (int x) {m_x = x;} void sety (int y) {m_y = y;} int Getarea () const;private:int m_x,m_y;};

  

---------------------------------------------------------------------------//file1.cpp#pragma Hdrstop#include <tchar.h> #include <iomanip> #include "File2.h" #include <iostream>using namespace std;//----------- ----------------------------------------------------------------#pragma argsusedpoint::P oint (int x,int y) {m_x = X;m _y = y;} Point::~point () {}int point::getarea () const{   int x = m_x*m_x;   int y = m_y*m_y;   return (x+y);} int _tmain (int argc, _tchar* argv[]) {point P (n); Cout<<p.getx () <<SETW () <<p.gety () <<endl; P.setx (3);p. Sety (4); Cout<<p.getx () <<SETW () <<p.gety () <<endl;cout<<p.getarea () <<endl;getchar (); return 0;} //---------------------------------------------------------------------------

Run results

complex invocation of inline functions, embedded with other data members

 

File2.h
#include <iostream>classpoint{ Public: voidSetX (intX) {m_x =x;} inline functionsvoidSety (intY) {m_y =y;} intGetX ()Const{returnm_x;} intGetY ()Const{returnm_y;}Private: intm_x; Defining x-Coordinatesintm_y; Defines the y-coordinate};classrectangle{ Public: Rectangle (intTopintLeftintBottomintRight ); ~Rectangle () {}intGetTop ()Const{returnM_top;} intGetLeft ()Const{returnM_left;} intGetbottom ()Const{returnM_bottom;} intGetRight ()Const{returnM_right;} Point Getupperleft () {returnM_upperleft;} Point Getlowerleft () {returnM_lowerleft;} Point Getupperright () {returnM_upperright;} Point Getlowerright () {returnM_lowerright;} voidSetupperleft (Point rt) {M_upperleft =RT;} voidSetlowerleft (Point rt) {M_lowerleft =RT;} voidSetupperright (Point rt) {M_upperright =RT;} voidSetlowerright (Point rt) {M_lowerright =RT;} voidSettop (intTop) {M_top =top;} voidSetleft (intleft) {M_left =Left ;} voidSetbottom (intBottom) {M_bottom =Bottom;} voidSetright (intright) {m_right=Right ;} intGetarea ()Const;Private: Point M_upperleft; Point M_upperright; Point M_lowerleft; Point M_lowerright; intM_top; intM_left; intM_bottom; intm_right;};
//---------------------------------------------------------------------------File1.cpp#pragmaHdrstop#include<tchar.h>#include"File2.h"//---------------------------------------------------------------------------#pragmaArgsusedusing namespacestd; Rectangle::rectangle (intTopintLeftintBottomintRight )//define four points of the rectangle {m_top=top; M_left=Left ; M_bottom=Bottom; M_right=Right ;    M_upperleft.setx (left);    M_upperleft.sety (top);    M_upperright.setx (right);    M_upperright.sety (top);    M_lowerleft.setx (left);    M_lowerleft.sety (bottom);    M_lowerright.setx (right); M_lowerright.sety (bottom);}intRectangle::getarea ()const//Calculate rectangular area{    intWidth = m_right-M_left; intHeight = M_top-M_bottom; return(width*Height);}int_tmain (intARGC, _tchar*argv[]) {Rectangle MyRectangle ( -, -, -, the); intArea =Myrectangle.getarea (); cout<<"Area :"<<Area<<Endl; cout<<"the coordinates in the upper-left corner are:"<<Myrectangle.getupperleft ().  GetX (); Gets the coordinates of the upper-left corner of the rectangle
GetChar (); return 0;}//---------------------------------------------------------------------------

The program is slightly difficult to understand is that the program gets the upper-left coordinate of the rectangle, when the program calls the Myrectangle.getupperleft () method, is actually called the File2.h file in the Getupperleft () method, the method returns a point, And if you want to get the x-coordinate from point, you need to call the Getx () method. Therefore, to get the x-coordinate of the upper-left corner of the rectangle, call Getupperleft () on the MyRectangle object and call Getx () on the returned value. The coordinates of the x obtained

VC + + Class object-oriented (iv)

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.