使用shared_ptr
当我使用原始指针时,在树上“遍历”非常容易,但是当我使用shared_ptr而不是内置指针时,情况并非如此。我的意思是我不能这样做(没有副作用):
shared_ptr<T> p(some_shared);
while (p->parent_)//here I'm assuming that the type pointed to has parent_ member
{
p = p->parent_;
}
这对我不起作用,因为它看起来像在分配给 p 时重置 p->parent 而这不是我想要的。
有什么线索吗?
编辑
这是真实的代码:
template<class Key_T, class Value_T>
class Node
{
public:
/*typedefs*/
#define ptr_type std::shared_ptr
typedef Key_T key_type;
typedef ptr_type<key_type> key_ptr;
typedef Value_T value_type;
typedef ptr_type<value_type> value_ptr;
typedef Colors color_type;
typedef color_type* color_raw_ptr;
typedef ptr_type<color_type> color_ptr;
typedef std::pair<key_ptr,value_ptr> data_type;
typedef ptr_type<data_type> data_ptr;
typedef Node<key_type,value_type> node_type;
typedef node_type* node_raw_ptr;
typedef ptr_type<node_type> node_ptr;
explicit Node()
{}
explicit Node(const key_type& key,
const value_type& value,
const color_type& color,
node_ptr parent = nullptr,
node_ptr left = nullptr,
node_ptr right = nullptr);
~Node()
{
cout << "Bye now";
}
const node_ptr& root()const
{
node_ptr tmp = node_ptr(this);
while (tmp->parent_)
{///this seems to reset resources
tmp = tmp->parent_;
}
return tmp;
}
private:
data_ptr data_;
color_ptr color_;
node_ptr parent_;
node_ptr left_;
node_ptr right_;
};
When I'm using raw pointers it is pretty easy to 'travers' up/down the tree, but when I've employed shared_ptr instead of built-in pointers it isn't so. I mean I just can't do (without side effects) just this:
shared_ptr<T> p(some_shared);
while (p->parent_)//here I'm assuming that the type pointed to has parent_ member
{
p = p->parent_;
}
This doesn't work for me because it looks like it resets p->parent when it assigns to p and that's not what I want.
Any clues?
Edit
This is real code:
template<class Key_T, class Value_T>
class Node
{
public:
/*typedefs*/
#define ptr_type std::shared_ptr
typedef Key_T key_type;
typedef ptr_type<key_type> key_ptr;
typedef Value_T value_type;
typedef ptr_type<value_type> value_ptr;
typedef Colors color_type;
typedef color_type* color_raw_ptr;
typedef ptr_type<color_type> color_ptr;
typedef std::pair<key_ptr,value_ptr> data_type;
typedef ptr_type<data_type> data_ptr;
typedef Node<key_type,value_type> node_type;
typedef node_type* node_raw_ptr;
typedef ptr_type<node_type> node_ptr;
explicit Node()
{}
explicit Node(const key_type& key,
const value_type& value,
const color_type& color,
node_ptr parent = nullptr,
node_ptr left = nullptr,
node_ptr right = nullptr);
~Node()
{
cout << "Bye now";
}
const node_ptr& root()const
{
node_ptr tmp = node_ptr(this);
while (tmp->parent_)
{///this seems to reset resources
tmp = tmp->parent_;
}
return tmp;
}
private:
data_ptr data_;
color_ptr color_;
node_ptr parent_;
node_ptr left_;
node_ptr right_;
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能像在创建共享指针时那样从中
创建共享指针,它会假定分配给它的指针的所有权 - 因此,当重新分配 tmp 时,这会删除该指针。
关于shared_ptr的主题: http://www .boost.org/doc/libs/1_47_0/libs/smart_ptr/sp_techniques.html#from_this
您需要创建共享指针总是这样:
那么如何获得指向 root() 的共享指针呢?如果您要使用boost,您将拥有shared_from_this: http:// www.boost.org/doc/libs/1_47_0/libs/smart_ptr/enable_shared_from_this.html
可以使用普通指针,或者将函数设置为类的外部函数或静态函数,将共享指针作为参数。
You cannot create a shared pointer from this as you do in
When you create a shared pointer, it assumes ownership of the pointer given to it - so this deletes this when tmp is reassigned.
On the subject of shared_ptr to this: http://www.boost.org/doc/libs/1_47_0/libs/smart_ptr/sp_techniques.html#from_this
You need to create shared pointers always like this:
So how do you get a shared pointer to root()? If you would use boost, you would have shared_from_this: http://www.boost.org/doc/libs/1_47_0/libs/smart_ptr/enable_shared_from_this.html
You can use normal pointers, or make the function external or static to the class, taking shared_ptr as an argument.
你有什么理由应该操作智能指针吗?由于您正在编写同步代码并且结构看起来并不懒惰,因此您并不真正关心所有权问题 - 这是一个愚蠢的遍历。所以使用原始指针是可以的。
Is there any reason that you should be manipulating smart pointers? Since you're writing synchronous code and the structure doesn't seem lazy you're not really concerned with issues of ownership -- this is a dumb traversal. So it's okay to use raw pointers.