Array like accss to the bits of a byte
So I have this class that a friend of mine wrote and there is one statement that confuses me:
Code:
#include <iostream>
using namespace std;
class twiddler;
class bits{
uint8_t& value;
int n;
friend class twiddler;
public:
bits(uint8_t& value, int n):value(value), n(n){}
bool operator[](int n){return (value & (1 << n)) != 0;}
void operator=(bool set_value){
if(set_value)
value |= (1 << n);
else
value &= ~(1 << n);
}
};
class twiddler{
uint8_t value;
public:
twiddler(uint8_t value):value(value){}
short operator*(){return (short)value;}
bits operator[](int n){return bits(value, n);}
};
int main(int argc, char** argv){
twiddler t(8);
t[0] = true;
cout << *t << endl;
}
In the public section of the twiddler class, why does the "operator[]" overload both have a return type of bits and returns bits, calling it's constructor. I'm not to sure how the methods of the bits class get called. It's like they are being called indirectly or something.