Friday, September 17, 2010

Volatile Member Functions

Just as you cannot call a const object's non-const member functions, you cannot call a volatile object's non-volatile member functions.

Here is a code example:

#include 
using namespace std;

class A
{
 public:
   void volatile_func() volatile
   {
      cout << "    volatile_func" << endl;
   }
   void non_volatile_func()
   {
      cout << "non_volatile_func" << endl;
   }
   void const_func() const
   {
      cout << "       const_func" << endl;
   }
   void non_const_func()
   {
      cout<< "   non_const_func" << endl;
   }
};

int main()
{
   volatile A aVolatile;
            A aNonVolatile;
   const    A aConst      = A();
            A aNonConst;
   aVolatile.volatile_func();
   ///////////////////////////////////////
   // The following line does not compile.
   //    aVolatile.non_volatile_func();
   ///////////////////////////////////////
   aNonVolatile.volatile_func();
   aNonVolatile.non_volatile_func();

   aConst.const_func();
   ///////////////////////////////////////
   // The following line does not compile.
   //    aConst.non_const_func();
   ///////////////////////////////////////
   aNonConst.const_func();
   aNonConst.non_const_func();
   return 0;
}

No comments:

Post a Comment