C++运算符重载练习:编写ipv4类

没什么,好久没玩 C++ 了。打算复习一下,编写个 ipv4 地址类娱乐一下。

IPv4地址

IPv4 地址是一个32位的数字,通常使用十进制和点.进行格式化,比如192.168.1.1。每一个部分都是一个8比特数,范围从0到255。可以使用4个 unsigned char 存储地址值。 为了能够直接从控制台输入和输出格式化的 IP 地址,重载 operator>>operator<<

一言不合上代码

#include <iostream>
#include <array>
#include <sstream>

class ipv4
{
   
    std::array<unsigned char, 4> data;
public:
    constexpr ipv4() : data{
    {
   0} } {
   }
    constexpr ipv4(unsigned char const a, unsigned char const b,
                   unsigned char const c, unsigned char const d):
        data{
    {
   a, b, c, d} } {
   }
    explicit constexpr ipv4(unsigned long a):
        data{
    {
    static_cast<unsigned char>((a >> 24) & 0xFF),
                static_cast<unsigned char>((a >> 16) & 0xFF),
                static_cast<unsigned char>((a >>  8) & 0xFF),
                static_cast<unsigned char
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值