天天看点

C++类的const, static 和inline成员函数(变量)

C++

类的声明中包含多个函数或变量的声明或定义,这些函数(变量)的声明或定义可以使用

const

static

inline

函数,分别称为常成员函数(变量),静态成员函数(变量)和内联成员函数,具有不同的意义和应用场景。:

  • 普通成员函数和变量

    在类中定义的不带有任何修饰符的成员函数或变量。

/****************************************
 * original_member.cpp                  *
 *                                      *
 * 普通成员函数和变量                   *
 ****************************************/

class Rectangle
{
private:
  int w;
  int h;
public:
  void Init(int _w, int _h);
  int Area();
};

void Rectangle::Init(int _w, int _h)
{
  w = _w;
  h = _h;
}

int Rectangle::Area()
{
  return w * h;
}

#include <iostream>

int main()
{
  Rectangle rect;
  rect.Init(,);
  std::cout<<"rect的面积为"<<rect.Area()<<std::endl;
  return ;
}
           
C++类的const, static 和inline成员函数(变量)
  • 静态成员函数(变量)

    带有

    static

    修饰符的函数或变量的声明。静态成员变量必须在类声明体外的某个地方(一般是定义文件中)进行定义。静态成员变量本质上是全局变量,在类的所有实例对象中共享一份。静态成员函数本质上是全局函数,并不具体作用与某个对象,不需要对象也可以访问。静态成员函数或变量的设计思路是将和某些类紧密相关的全局变量或函数写在类里面,使其看上去像一个整体,易于理解和维护。在静态成员函数中不能访问非静态成员变量,也不能调用非静态成员函数。

    在类的对象存在的情况下,可以像访问普通成员函数和变量一样访问静态成员或变量,也可以直接用

    类名::成员函数(变量)

    的方式访问静态成员变量或调用静态成员函数
/****************************************
 * static_member.cpp                    *
 *                                      *
 * 静态成员函数和变量                   *
 ****************************************/
#include <iostream>

class Rectangle
{
private:
  int w;
  int h;

private:
  static int rectNum;

public:
  Rectangle(int _w, int _h)
  {
    w = _w;
    h = _h;
    rectNum++;
  }

  int Area();

  static void PrintRectNum()
  {
    std::cout<<"现在有"<<rectNum<<"个长方形"<<std::endl;
  }
};

int Rectangle::Area()
{
  return w * h;
}

int Rectangle::rectNum = ;

int main()
{
  Rectangle rect(,);
  std::cout<<"rect的面积为"<<rect.Area()<<std::endl;

  Rectangle rect1(,);
  std::cout<<"rect1的面积为"<<rect1.Area()<<std::endl;

  Rectangle rect2(,);
  std::cout<<"rect2的面积为"<<rect2.Area()<<std::endl;

  rect2.PrintRectNum();

  Rectangle::PrintRectNum();

  return ;
}
           
C++类的const, static 和inline成员函数(变量)
  • 常成员函数

    函数参数列表后带有

    const

    修饰符的函数。

    函数类型 函数名(参数列表) const

    常量成员函数只能由常量对象来访问,且在成员函数中不能修改对象的成员变量。若确实要修改,需要将成员变量添加

    mutable

    修饰符。类中也可以使用常量作为属性。
  • 内联成员函数

    在类声明体中定义的成员函数,或者在类声明之外使用了

    inline

    修饰符定义的成员函数称为内联函数。与一般函数不同,内联函数不是发生调用转移,而是在编译时将函数体替换每个调用处的函数名。但是声明为

    inline

    的函数不一定就被当作内联函数处理,编译器会根据函数的定义进行决断。
/****************************************
 * inline_member.cpp                    *
 *                                      *
 * 内联成员函数和变量                   *
 ****************************************/

class Rectangle
{
private:
  int w;
  int h;
public:

  void Init(int _w, int _h)
  {
    w = _w;
    h = _h;
  }

  inline int Area();
};


int Rectangle::Area()
{
  return w * h;
}

#include <iostream>

int main()
{
  Rectangle rect;
  rect.Init(,);
  std::cout<<"rect的面积为"<<rect.Area()<<std::endl;
  return ;
}
           
C++类的const, static 和inline成员函数(变量)

参考文献

  1. Bjarne Stroustrup著,裘宗燕译. C++程序设计语言(特别版).机械工业出版社 2009
  2. https://www.coursera.org/course/pkupop
  3. http://baike.baidu.com/link?url=57M7zFAkdQy_f8q9egc7Ka8Azd3YoOK8-_5Oawe2QWCDIfpCeM1637pwNDS_pSrU1X_2EIs8BLF5BoViKxBB1K

继续阅读