本帖最后由 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;
}
测试结果如下:
|