我想做的是:
#include <memory>
class autostr : public std::auto_ptr<char>
{
public:
autostr(char *a) : std::auto_ptr<char>(a) {}
autostr(autostr &a) : std::auto_ptr<char>(a) {}
// define a bunch of string utils here...
};
autostr test(char a)
{
return autostr(new char(a));
}
void main(int args, char **arg)
{
autostr asd = test('b');
return 0;
}
(我实际上也有处理数组的 auto_ptr 类的副本,但同样的错误适用于 stl 类)
使用 GCC 4.3.0 的编译错误是:
main.cpp:152:错误:没有匹配的函数调用“autostr::autostr(autostr)” main.cpp:147:注意:候选人是:autostr::autostr(autostr&) main.cpp:146:注意:autostr::autostr(char*)
我不明白为什么它不匹配 autostr 参数作为 autostr(autostr&) 的有效参数。