Archived

This topic is now archived and is closed to further replies.

gimp

'boost::weak_ptr<T>' does not have an overloaded member 'operator ->'

Recommended Posts

Really? Doesn''t that make it less useful, at least from a code readability POV? Maybe I''m not using this right:
  
//.h

~~
	boost::shared_ptr<CCommand> m_DataSource;
~~

//.cpp

inline boost::weak_ptr<CCommand> CVisualComponent::GetDataSource(void) const
{
	assert(m_DataSource.get());
	return boost::weak_ptr<CCommand>(m_DataSource);
}
  
later in a derived class: GetDataSource().get()->Execute(m_Text); Am I using it correctly? I would have hoped for smart pointer to be less intrusive an in all instances just pretend to be the pointer they manage. I looked over the Doco, they don''t mention anything about and -> evilness so I''m left wondering if this is how I''m meant to use it... Many thanks, Chris Brodie http:\\fourth.flipcode.com

Share this post


Link to post
Share on other sites
quote:
Original post by gimp
Whats a weak pointer used for then may I ask? I just don't get it yet...

It allows you to get a handle to a pointer that is managed elsewhere, and to track the validity of that pointer. A weak_ptr will generally be constructed from an owning smart pointer, and will retain a connection with that smart pointer so that it knows when the pointed to object is no longer valid. Say you want a class to retain a pointer to an object, but you do not want that class to affect the lifetime of the object, you would use a weak_ptr, and transform that into either a shared_ptr or a "dumb pointer" (via get) whenever you want to access the object - first ensuring that the object is still alive.

[edited by - SabreMan on August 13, 2002 7:33:18 AM]

Share this post


Link to post
Share on other sites