c++面向对象 之 内联函数 this 静态成员

1,内联函数

如果一个函数是内联的,那么在编译时,编译器会把该函数的代码副本放置在每个调用该函数的地方。用inline指定,内联函数通常短小精悍没有while和for循环,能够帮助提升程序执行的速度

#include <iostream>
 
using namespace std;

inline int Max(int x, int y)
{
   return (x > y)? x : y;
}

// 程序的主函数
int main( )
{

   cout << "Max (20,10): " << Max(20,10) << endl;    //在这里调用Max的时候,编译器会把整个函数的代码copy过来
   cout << "Max (0,200): " << Max(0,200) << endl;
   cout << "Max (100,1010): " << Max(100,1010) << endl;
   return 0;
}

2,this指针

#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,指向类的指针

#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
   Box *ptrBox;                // Declare pointer to a class.
   double var = 2;
   double* ip =&var;
   // 保存第一个对象的地址
   ptrBox = &Box1;
   cout<<"平时调用类"<<Box1.Volume()<<endl;
   // 现在尝试使用成员访问运算符来访问成员
   cout << "Volume of Box1: " << ptrBox->Volume() << endl;    //如果是指针指向的类,本应该这样调用,*(ptrBox).Volume(),但是也可以用比较简练的方式:->
   cout<<"daqing ptrBox:"<<ptrBox<<endl;
   cout<<"address value"<<ip<<*ip<<endl;
   // 保存第二个对象的地址
   ptrBox = &Box2;

   // 现在尝试使用成员访问运算符来访问成员
   cout << "Volume of Box2: " << ptrBox->Volume() << endl;
  
   return 0;
}

 上面的例子,返回结果:

Constructor called.
Constructor called.
平时调用类5.94
Volume of Box1: 5.94
daqing ptrBox:0x7ffe09478430
address value0x7ffe094784182
Volume of Box2: 102

4,类的静态成员 static

static静态函数标志可以这样来理解:

函数内部定义的变量,在程序执行到它的定义处时,编译器为它在栈上分配空间,然后,函数在栈上分配的空间在此函数执行结束时会释放掉,这样就产生了一个问题: 如果想将函数中此变量的值保存至下一次调用时,如何实现? 最容易想到的方法是定义一个全局的变量,但定义为一个全局变量有许多缺点,最明显的缺点是破坏了此变量的访问范围(使得在此函数中定义的变量,不仅仅受此函数控制)。所以需要static这样属于局部但是又能够全局访问的变量。

#include <iostream>
 
using namespace std;
 
class Box
{
   public:
      static int objectCount;    //静态变量
      // 构造函数定义
      Box(double l=2.0, double b=2.0, double h=2.0)
      {
         cout <<"Constructor called." << endl;
         length = l;
         breadth = b;
         height = h;
         // 每次创建对象时增加 1
         objectCount++;
      }
      double Volume()
      {
         return length * breadth * height;
      }
   private:
      double length;     // 长度
      double breadth;    // 宽度
      double height;     // 高度
};
 
// 初始化类 Box 的静态成员
int Box::objectCount = 0;    //访问或者修改静态成员可以不用实例,直接上就行,因为静态类是为类服务的(不支持this),静态属性只能初始化一次
 
int main(void)
{
   Box Box1(3.3, 1.2, 1.5);    // 声明 box1
   Box Box2(8.5, 6.0, 2.0);    // 声明 box2
 
   // 输出对象的总数
   cout << "Total objects: " << Box::objectCount << endl;
 
   return 0;
}

以上实例返回结果:

Constructor called.
Constructor called. Total objects: 2

静态类的方法也是一样的,不用实例化直接调用就是了,和php差不多

猜你喜欢

转载自www.cnblogs.com/0-lingdu/p/11177498.html