| 1 |
#include <iostream> |
| 2 |
#include <cstdint> // uint8_t |
| 3 |
|
| 4 |
class Byte |
| 5 |
{ |
| 6 |
friend std::istream &operator>>(std::istream &out, Byte &byte); |
| 7 |
friend std::ostream &operator<<(std::ostream &out, Byte const &byte); |
| 8 |
|
| 9 |
uint8_t d_byte; |
| 10 |
|
| 11 |
public: |
| 12 |
Byte(); // CC available by default |
| 13 |
Byte(uint8_t byte); |
| 14 |
|
| 15 |
Byte &operator=(Byte const &rhs) = default; |
| 16 |
Byte &operator=(uint8_t rhs); |
| 17 |
|
| 18 |
operator uint8_t &(); |
| 19 |
operator uint8_t const &() const; |
| 20 |
}; |
| 21 |
|
| 22 |
inline Byte::Byte() |
| 23 |
: |
| 24 |
d_byte(0) |
| 25 |
{} |
| 26 |
|
| 27 |
inline Byte::Byte(uint8_t byte) |
| 28 |
: |
| 29 |
d_byte(byte) |
| 30 |
{} |
| 31 |
|
| 32 |
inline Byte &Byte::operator=(uint8_t rhs) |
| 33 |
{ |
| 34 |
d_byte = rhs; |
| 35 |
return *this; |
| 36 |
} |
| 37 |
|
| 38 |
inline Byte::operator uint8_t &() |
| 39 |
{ |
| 40 |
return d_byte; |
| 41 |
} |
| 42 |
|
| 43 |
inline Byte::operator uint8_t const &() const |
| 44 |
{ |
| 45 |
return d_byte; |
| 46 |
} |
| 47 |
|
| 48 |
inline std::ostream &operator<<(std::ostream &out, Byte const &byte) |
| 49 |
{ |
| 50 |
return out << static_cast<uint16_t>(byte.d_byte); |
| 51 |
} |
| 52 |
|
| 53 |
std::istream &operator>>(std::istream &in, Byte &byte) |
| 54 |
{ |
| 55 |
size_t value; |
| 56 |
in >> value; |
| 57 |
byte.d_byte = value; |
| 58 |
|
| 59 |
return in; |
| 60 |
} |
| 61 |
|
| 62 |
using namespace std; |
| 63 |
|
| 64 |
int main() |
| 65 |
{ |
| 66 |
Byte b1; |
| 67 |
Byte b2{ 12 }; |
| 68 |
Byte b3{ b2 }; |
| 69 |
|
| 70 |
b1 += 13; |
| 71 |
b1 = 65; |
| 72 |
|
| 73 |
b1 <<= 1; |
| 74 |
b1 >>= 1; |
| 75 |
b1 |= 1; |
| 76 |
|
| 77 |
uint8_t u8 = b1; |
| 78 |
|
| 79 |
cout << b1 << ' ' << b3 << ' ' << u8 << '\n'; |
| 80 |
} |