OOPortal OOPortal


C++ Class Construct  «Prev  Next»
Lesson 10 Defining static and const member functions
ObjectiveDefine static and const member functions.

Defining static and const Member Functions in C++

Define Static Member Functions
C++ allows static and const member functions.

static member function syntax

A static member function has the modifier static precede the return type inside the class declaration. A definition outside the class must not have the static modifier.

class foo {
.....
   static int  foo_fcn();  //static goes first
.....
};

int foo::foo_fcn()//no static keyword here
{  /* definition */ }


const member function syntax

A const member function has the modifier const follow the argument list inside the class declaration. A definition outside the class must have the const modifier.

class foo {
.....
   int  foo_fcn() const;
.....
};

int foo::foo_fcn() const   //const keyword needed
{  /* definition */ }