吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 472|回复: 1
收起左侧

[C&C++ 原创] 基于C++11封装一个简单的路径拼接类

[复制链接]
licks 发表于 2024-3-22 08:38
本帖最后由 licks 于 2024-3-25 11:12 编辑
#include <iostream>
class path
{
public:
    path() = default;
    explicit path(const char* str) : path_(str) {}
    path(const path& other) = default;
    path(path&& other) noexcept : path_(std::move(other.path_)) {}
    path& operator=(const path& other)
    {
        if (this != &other)
        {
            path_ = other.path_;
        }
        return *this;
    }
    path& operator=(path&& other) noexcept
    {
        if (this != &other)
        {
            path_ = std::move(other.path_);
        }
        return *this;
    }
    path& operator/(const path& other)
    {
        path_ += '/';
        path_ += other.path_;
        return *this;
    }
    path& operator/(const char* str)
    {
        path_ += '/';
        path_ += str;
        return *this;
    }
    path& operator/(const std::string& str)
    {
        path_ += '/';
        path_ += str;
        return *this;
    }
    ~path() = default;

    const char* c_str() const { return path_.c_str(); }
private:
    std::string path_;
};


下面是测试代码:
int main()
{
    path p1("/usr");
    p1 = p1 / "local";
    std::cout << "new path=" << p1.c_str() << std::endl;
    return 0;
}


测试结果如下:

Dingtalk_20240322083745.jpg

免费评分

参与人数 1吾爱币 +7 热心值 +1 收起 理由
爱飞的猫 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

seawaycao 发表于 2024-3-22 12:22
建议改一下名字:路径拼接
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-15 06:19

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表