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

Started by
2 comments, last by gimp 21 years, 8 months ago
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
Chris Brodie
Advertisement
weak_ptr is a non-owning smart pointer. It sounds like you want an owning smart pointer, which would be shared_ptr.
Whats a weak pointer used for then may I ask? I just don''t get it yet...

Chris Brodie
http:\\fourth.flipcode.com
Chris Brodie
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]

This topic is closed to new replies.

Advertisement