更改函数以返回指针
我对 C++ 很陌生,我已经编写了下面的类和函数:
class Person {
public:
boost::shared_ptr<Details> GetDetails(const std::string& name) const;
...
private:
std::map<std::string, boost::shared_ptr<Details> > map;
...
};
inline
boost::shared_ptr<Details>
Person::GetDetails(const std::string& name) const {
return map.find(name)->second;
}
这工作正常,但我被告知让函数返回指针,如果查找失败则返回 NULL。
我尝试了几件事,其中之一是:
class Person {
public:
boost::shared_ptr<Details> *GetDetails(const std::string& name) const;
...
private:
std::map<std::string, boost::shared_ptr<Details> > map;
...
};
inline
boost::shared_ptr<Details>
*Person::GetDetails(const std::string& name) const {
if (!map.find(name)->first.empty()) {
return *map.find(name)->second;
}
return NULL;
}
这给了我错误:
error: cannot convert 'Details' to 'boost::shared_ptr<Details>*' in return
我不完全确定在这里做什么。任何帮助或资源都会有很大帮助。
谢谢。
I'm quite new to C++ and I have written the class and function below:
class Person {
public:
boost::shared_ptr<Details> GetDetails(const std::string& name) const;
...
private:
std::map<std::string, boost::shared_ptr<Details> > map;
...
};
inline
boost::shared_ptr<Details>
Person::GetDetails(const std::string& name) const {
return map.find(name)->second;
}
This works fine, but I have been told to make the function return the pointer instead, and return NULL if the find fails.
I have tried a couple of things, one of which is:
class Person {
public:
boost::shared_ptr<Details> *GetDetails(const std::string& name) const;
...
private:
std::map<std::string, boost::shared_ptr<Details> > map;
...
};
inline
boost::shared_ptr<Details>
*Person::GetDetails(const std::string& name) const {
if (!map.find(name)->first.empty()) {
return *map.find(name)->second;
}
return NULL;
}
which gives me the error:
error: cannot convert 'Details' to 'boost::shared_ptr<Details>*' in return
I'm not entirely sure what to do here. Any help or resources would be of great help.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想要返回共享指针的地址,因此您需要使用
&
,而不是*
。请注意,处理指向共享指针的指针有点奇怪。如果找不到该项目,为什么不直接返回一个空的共享指针呢?
You want to return the address of a shared pointer, so you need to use
&
, not*
.Note that dealing with pointers to shared-pointers is a little bit weird. Why not just return an empty shared-pointer if you can't find the item?
您需要返回空的
boost::shared_ptr
而不是这个
另外返回指向
shared_ptr
的指针是个坏主意。你以前的版本看起来更好。You need to return empty
boost::shared_ptr<Details>
instead of this
Also returning a pointer to
shared_ptr
is bad idea. You previous version looks better.您应该在访问
first
或second
之前测试 find 得到的内容:但不要返回裸指针。这会很危险。
基本原理:
find()
在最后一个条目之后返回map.end()
迭代器。所以没有入口可以访问。map.find(name)->first.empty()
将是未定义的行为,因为现在地图末尾可能有空字符串。shared_ptr
负责所有内存管理。返回原始指针可能会毁掉这一切。You should test what you get by find before accessing
first
orsecond
:but don't return naked pointers. It would be dangerous.
Rationale:
find()
returnsmap.end()
iterator after the last entry. So there is no entry to access.map.find(name)->first.empty()
would be undefined behavior, since there may be now empty string at the end of your map.shared_ptr
does all the memory management. Returning raw pointers could ruin all that.