Understanding boost::shared_ptr method

K

ketan

Hi,

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

template<class T> class shared_ptr
{
.....
template<class Y>
shared_ptr(shared_ptr<Y> const & r, boost::detail::const_cast_tag):
px(const_cast<element_type *>(r.px)), pn(r.pn)
{
}

....
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS

private:

template<class Y> friend class shared_ptr;
template<class Y> friend 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
 
D

David Harmon

On 6 Sep 2006 16:23:58 -0700 in comp.lang.c++, "ketan"
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS

private:

template<class Y> friend class shared_ptr;
template<class Y> friend 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)
 
K

ketan

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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top