More on 'smart pointers'

Discussion in 'C++' started by Keith Willis, Nov 8, 2007.

  1. Keith Willis

    Keith Willis Guest

    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?
    --
    PGP key ID 0xEB7180EC
     
    Keith Willis, Nov 8, 2007
    #1
    1. Advertising

  2. Keith Willis

    Keith Willis Guest

    On Thu, 08 Nov 2007 16:15:18 +0000, Keith Willis <>
    wrote:

    >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!
    --
    PGP key ID 0xEB7180EC
     
    Keith Willis, Nov 8, 2007
    #2
    1. Advertising

  3. Keith Willis wrote:

    > On Thu, 08 Nov 2007 16:15:18 +0000, Keith Willis <>
    > wrote:
    >
    >>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!


    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
     
    Paul Brettschneider, Nov 8, 2007
    #3
  4. Keith Willis

    James Kanze Guest

    On Nov 8, 5:15 pm, Keith Willis <> wrote:
    > 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).

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Nov 9, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael
    Replies:
    4
    Views:
    415
    Matt Hammond
    Jun 26, 2006
  2. Chad

    More on pointers to pointers.

    Chad, Mar 31, 2006, in forum: C Programming
    Replies:
    6
    Views:
    286
    Dave Vandervies
    Apr 1, 2006
  3. MotoK
    Replies:
    59
    Views:
    1,814
    Keith Thompson
    Sep 15, 2006
  4. n2xssvv g02gfr12930

    Smart pointers and member function pointers

    n2xssvv g02gfr12930, Nov 26, 2005, in forum: C++
    Replies:
    3
    Views:
    471
    n2xssvv g02gfr12930
    Nov 27, 2005
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    679
Loading...

Share This Page