c ++ inline function for this static member within the object

1, inline

If a function is inline, then at compile time, the compiler will copy the code for the function of each placed where the function is called. Be specified inline, inline functions not typically dapper while and for loop, can help improve the speed of program execution

#include <the iostream> 
 
the using namespace STD; 

inline int Max ( int X, int Y) 
{ 
   return (X> Y)? X: Y; 
} 

// main function program 
int main () 
{ 

   COUT << "Max (20 is , 10): "<< Max (20,10) << endl; // Max's called here, the compiler will copy the entire function of the code came 
   cout <<" Max (0,200): "<< Max ( 0,200) << endl; 
   COUT << "Max (100,1010):" << Max (100,1010) << endl;
    return 0 ; 
}

2, this pointer

#include <iostream>
 
using namespace std;
 
class Box
{
   public:
      // 构造函数定义
      Box(double l=2.0, double b=2.0, double h=2.0)
      {
         cout <<"Constructor called." << endl;
         length = l;
         breadth = b;
         height = h;
      }
      double Volume()
      {
         return length * breadth * height;
      }
      int compare(Box box)
      {
         return this->Volume() > box.Volume();    //重点是this->Volume()能够得到类本身的乘积
      }
   private:
      double length;     // Length of a box
      double breadth;    // Breadth of a box
      double height;     // Height of a box
};
 
int main(void)
{
   Box Box1(3.3, 1.2, 1.5);    // Declare box1
   Box Box2(8.5, 6.0, 2.0);    // Declare box2
 
   if(Box1.compare(Box2))
   {
      cout << "Box2 is smaller than Box1" <<endl;
   }
   else
   {
      cout << "Box2 is equal to or larger than Box1" <<endl;
   }
   return 0;
}

3, a pointer pointing to the class

#include <iostream>
 
using namespace std;

class Box
{
   public:
      // 构造函数定义
      Box(double l=2.0, double b=2.0, double h=2.0)
      {
         cout <<"Constructor called." << endl;
         length = l;
         breadth = b;
         height = h;
      }
      double Volume()
      {
         return length * breadth * height;
      }
   private:
      double length;     // Length of a box
      double breadth;    // Breadth of a box
      double height;     // Height of a box
};

int main(void)
{
   Box Box1(3.3, 1.2, 1.5);    // Declare box1
   Box Box2(8.5, 6.0, 2.0);    // Declare box2
   * PtrBox Box;                 // the Declare A pointer to class. 
   Double  var = 2 ;
    Double * IP = & var ;
    // save the first address of an object 
   ptrBox = & Box1; 
   COUT << "usually calls the class" << Box1. Volume () << endl; 
   // now try to use the member access operator to access members 
   COUT << " Volume of Box1: " << ptrBox-> Volume () << endl; // if it is a pointer to the class, the present should this call, * (ptrBox) .Volume () , but you can also use more concise way: -> 
   cout << " Daqing ptrBox: " << ptrBox << endl; 
   cout <<"address value"* ip ip << << << endl;
    // save the second object address 
   ptrBox = & Box2; 

   // now try to use the member access operator to access members 
   cout << " Volume of Box2: " << ptrBox- > Volume () << endl; 
  
   return  0 ; 
}

 Examples of the above, returns the result:

Called constructor.
Constructor Called.
Usually the calling class 5.94
Volume of Box1: 5.94
Daqing ptrBox: 0x7ffe09478430
address value0x7ffe094784182
Volume of Box2: 102

4, static member of a class of static

static static function logo can be understood:

When the internal functions defined variables, the routine proceeds to its definition, the compiler it allocates space on the stack, and then, spatial function allocated on the stack at the end of the execution of the function will be freed, thus creating a question: If you want to save the function value of this variable to the next call, how to achieve? Easiest conceivable define a global variable, but defined as a global variable has a number of drawbacks, the most obvious disadvantage is, destroys this variable range (such variables defined in this function, not only by this control function ). It is necessary to belong to such a static local variable but it can be globally accessible.

#include <the iostream> the using namespace STD; class Box 
{ public :
       static int objectCount;     // static variables
       // constructor defines 
      Box ( Double L = 2.0 , Double B = 2.0 , Double H = 2.0 ) 
      { 
         COUT << " the Constructor . Called " << endl; 
         length = L; 
         the breadth = B; 
         height
 
 
 

    = H;
          // incremented one object creation 
         objectCount ++ ; 
      } 
      Double Volume () 
      { 
         return length * * the breadth height; 
      } 
   Private :
       Double length;      // length 
      Double the breadth;     // width 
      Double height;      // height 
}; 
 
// static member initialization class Box of 
int Box :: objectCount = 0 ;     // access or modify the static member can not instances, directly on the line, because the static class is a class of service (does not support this), can only initialize static properties once 
 
int main ( void) 
{ 
   Box Box1 ( 3.3 , 1.2 , for 1.5 );     // declare box1 
   Box Box2 ( 8.5 , 6.0 , 2.0 );     // declare BOX2 
 
   // output target total 
   COUT << " the Total Objects: " << Box :: objectCount << endl; 
 
   return  0 ; 
}

Examples of the above returns the result:

Constructor Called . Constructor Called . Total Objects : 2
  

method Static class is the same, do not call directly instantiate it wants, and almost php

 

Guess you like

Origin www.cnblogs.com/0-lingdu/p/11177498.html