Understanding boost::shared_ptr method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ketan

    #1

    Understanding boost::shared_ptr method

    Hi,

    While reading the Boost::shared_p tr implementation, I came across this
    method

    template<class Tclass shared_ptr
    {
    .....
    template<class Y>
    shared_ptr(shar ed_ptr<Yconst & r, boost::detail:: const_cast_tag) :
    px(const_cast<e lement_type *>(r.px)), pn(r.pn)
    {
    }

    ....
    #ifndef BOOST_NO_MEMBER _TEMPLATE_FRIEN DS

    private:

    template<class Yfriend class shared_ptr;
    template<class Yfriend class weak_ptr;


    #endif

    T * px; // contained pointer
    boost::detail:: shared_count pn; // reference counter
    };

    Does it mean that px and pn are public members ? As it seems from its
    usage in dynamic casting.

    My understanding is correct or am I missing anything?

    -ketan

  • David Harmon

    #2
    Re: Understanding boost::shared_p tr method

    On 6 Sep 2006 16:23:58 -0700 in comp.lang.c++, "ketan"
    <ketan.mehta@gm ail.comwrote,
    >#ifndef BOOST_NO_MEMBER _TEMPLATE_FRIEN DS
    >
    >private:
    >
    template<class Yfriend class shared_ptr;
    template<class Yfriend class weak_ptr;
    >
    >
    >#endif
    >
    T * px; // contained pointer
    boost::detail:: shared_count pn; // reference counter
    >};
    >
    >Does it mean that px and pn are public members ? As it seems from its
    >usage in dynamic casting.
    It means that px and pn are public when compiled with a
    configuration for a compiler that doesn't support member template
    friends. Private otherwise. You are not supposed to take unfair
    advantage of that.
    >My understanding is correct or am I missing anything?
    My copy (boost 1.32) contains the following comment immediately
    before the above snippet:

    // Tasteless as this may seem, making all members public allows
    member templates
    // to work in the absence of member template friends. (Matthew
    Langston)

    Comment

    • ketan

      #3
      Re: Understanding boost::shared_p tr method

      It means that px and pn are public when compiled with a
      configuration for a compiler that doesn't support member template
      friends. Private otherwise. You are not supposed to take unfair
      advantage of that.
      OK. I understood that and sorry for missing the comment ! Yes, it is
      there in my copy of code also. So it means that by making typename <Y>
      shadred_ptr, we get two class with different types and being member can
      access privates!. Cool,
      thx for helping out.

      ketan

      Comment

      Working...