类 / 下标(索引器方法)

#include <iostream>
#include "math.h"
using namespace std


class PowerOfTwo {
public:
    int operator [] (int i)  {
        return (intpow(2, i);
    }
};

PowerOfTwo power2;
int p2 = power2[2];
//p2 is 4
int p8 = power2[8];
//p8 is 256
int p16 = power2[16];
//p16 is 65536

cout << "p2 is " << p2 << endl;
cout << "p8 is " << p8 << endl;
cout << "p16 is " << p16 << endl;