More on 'smart pointers'

K

Keith Willis

Moving along from my experiments with auto_ptr, I threw together a
custom smart pointer template based on stuff I saw at the GOTW site.
It all works swimmingly, except when I need to pass the underlying
pointer to a function like memset() or whatever. The template has
overloads for operator->() and operator*() like this:

template <class T> class SmartPtr
{
<snip>
T& operator*() const
{ return *p_; }

T* operator->() const
{ return p_; }
<snip>
}

This is fine and I can access members using the normal p_->member
syntax. But what about when I want to pass the pointer to a function,
like this:

memset(p_, value, sizeof *p_);

This gives me an error complaining that it can't convert a
SmartPtr<whatever> to a void*. At present I've worked around it by
adding a method to the template which explicitly returns the address:

template <class T> class SmartPtr
{
<snip>
T& operator*() const
{ return *p_; }

T* operator->() const
{ return p_; }

T* Addr() const
{ return p_; }
<snip>
}

which lets me do:

memset(p_.Addr(), value, sizeof *p_);

Is this the only/best way around the problem?
 
K

Keith Willis

This is fine and I can access members using the normal p_->member
syntax. But what about when I want to pass the pointer to a function,
like this:

memset(p_, value, sizeof *p_);

Brief self-follow up; I've discovered a workaround of sorts in that I
can say:

memset(&(*p_), value, sizeof *p_);

which is hardly elegant!
 
P

Paul Brettschneider

Keith said:
Brief self-follow up; I've discovered a workaround of sorts in that I
can say:

memset(&(*p_), value, sizeof *p_);

which is hardly elegant!

I've never had to use smart pointers, but without the parens: &*p_ doesn't
look inelegant, IMHO.

If you care, maybe you should just add an operator void*() to your pointer
class:
operator void*() const
{ return p_; };
or a operator T*():
operator T*() const
{ return p_; };
(untested)

HTH
 
J

James Kanze

Moving along from my experiments with auto_ptr, I threw
together a custom smart pointer template based on stuff I saw
at the GOTW site. It all works swimmingly, except when I need
to pass the underlying pointer to a function like memset() or
whatever. The template has overloads for operator->() and
operator*() like this:
template <class T> class SmartPtr
{
<snip>
T& operator*() const
{ return *p_; }
T* operator->() const
{ return p_; }
<snip>
}
This is fine and I can access members using the normal
p_->member syntax. But what about when I want to pass the
pointer to a function, like this:
memset(p_, value, sizeof *p_);
This gives me an error complaining that it can't convert a
SmartPtr<whatever> to a void*. At present I've worked around
it by adding a method to the template which explicitly returns
the address:
template <class T> class SmartPtr
{
<snip>
T& operator*() const
{ return *p_; }

T* operator->() const
{ return p_; }

T* Addr() const
{ return p_; }
<snip>
}
which lets me do:
memset(p_.Addr(), value, sizeof *p_);
Is this the only/best way around the problem?

That's the usual solution (although I can't think of a case
where I'd want to memset something that was managed by a smart
pointer).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top