Static function variable : Static « Language « C++






Static function variable

Static function variable

#include <iostream>
using namespace std;
void printMessage(void);  

int main ()
{
   char choice;
   do {
      cout << "Enter Q to quit, any other character to continue: ";
      cin >> choice;
      if (choice == 'Q')
         cout << "Input stopped";
      else
         printMessage(); 
      } while (choice != 'Q');
   return 0;
}
void printMessage (void)
{
   static int times = 0;
   times++;
   cout << "This function called " << times << " times\n";
}

           
       








Related examples in the same category

1.Static function and static variableStatic function and static variable
2.Static Member Functions: its strictionsStatic Member Functions: its strictions
3.Static member functions: 'preinitialize' private static dataStatic member functions: 'preinitialize' private static data
4.A static member variable example.A static member variable example.
5.Use a static member variable independent of any object.Use a static member variable independent of any object.
6.Init static data before object creationInit static data before object creation
7.A shared resource example.A shared resource example.
8.Usage and effect of a static data memberUsage and effect of a static data member