假设我有一个基本路径D:\files和一个绝对路径D:files\images\1.jpg。有没有一种方法可以将此绝对路径转换为相对于基本路径的相对路径?
据说构造函数不返回任何内容。但是如果构造函数不返回任何内容,那么这段代码是如何工作的:
*this=classname{args};。我希望有人能让我了解幕后实际情况。
完整代码:
#include <iostream>
using namespace std;
class hell {
private:
int a{}, b{};
public:
hell(int _a = 7, int _b = 8) : a{ _a }, b{ _b } {}
void print() {
cout << a << endl << b << endl;
}
void change() {
*this = hell(4, 5);
}
};
int main() {
hell h;
h.print();
h.change();
h.print();
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有这个示例代码:
std::unique_ptr<Base> some_function() {
//I cannot use unique ptr here becuase it will get freed when the function return i guess
Derived* derived = new Derived;
return static_cast<std::unique_ptr<Base>>(derived);
}
Run Code Online (Sandbox Code Playgroud)
使用static_cast这里是一个好的解决方案吗?还有其他选择返回吗unique_ptr?
和
return std::unique_ptr<Command>(derived);
Run Code Online (Sandbox Code Playgroud)
如果我像这样返回,由于 ptr 是任意的,它会在返回表达式的末尾被释放吗?
如果我不想在 中使用原始指针,解决方法是什么Derived* derived = new Derived;?