如何使用C++在类中定义函数?

H4c*_*0rD 1 c++

如何使用C++在类中定义函数?

Bri*_*ndy 17

有2个答案:

1)结合声明和定义:

class C
{
public:
  //declaration and definition of f
  void f()
  {
  }

};
Run Code Online (Sandbox Code Playgroud)

2)分离声明和定义:

class C
{
public:
  //declaration of f
  void f();
};

//definition of f
void C::f()
{
}
Run Code Online (Sandbox Code Playgroud)

通常在选项#2中,您将声明分隔为头文件和源文件中的定义.

  • 这就是答案(+1).也许接受它; 没有人会发布任何更多,因为这涵盖了所有. (2认同)