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