Thursday, November 25, 2010

operator void *()

The following code compiles:

#include <iostream>
using namespace std;
struct MyStrm
{
  MyStrm()
  {
    d_cnt = 5;
  }
  operator void *()
  {
    return d_cnt ? &d_cnt : 0;
  }
  int get() {
    return d_cnt--;
  }
  int d_cnt;
};

int main()
{
  MyStrm myStrm;
  while (myStrm)
    cout << myStrm.get() << endl;
  return 0; 
}

The above program prints out the following:

5
4
3
2
1

No comments:

Post a Comment