Template problem: Why does this work?

Discussion in 'C++' started by Thomas Lorenz, Oct 15, 2004.

  1. Hello,

    im working on a problem related to smart pointers. If the following is
    legal C++

    A* a(new A());
    B* b;
    b = a;

    I want to be able to do the following:

    Ptr<A> a(new A());
    Ptr<B> b;
    b = a;

    with equivalent semantics. Eventually, I came up with the following
    "solution", involving a strange friend declaration:

    template<class T>
    class Ptr {
    private:
    T* m_pObject;
    public:

    // Constructors etc. removed for simplicity

    template<class U>
    inline friend void copy(Ptr<T>& to, Ptr<U>& from)
    {
    to.m_pObject = from.m_pObject;
    };

    // Curious trick
    template<class U>
    friend void copy(Ptr<U>&, Ptr<T>&);

    template<class U>
    inline Ptr(Ptr<U>& rhs)
    : m_pObject(0)
    {
    copy(*this, rhs);
    }

    template<class U>
    inline Ptr<T> operator=(Ptr<U>& rhs)
    {
    copy(*this,rhs);
    return *this;
    }
    }

    This works quite well using the MS 7.0 compiler. But I really don't know
    why this "trick" enables copy() to access private members of Ptr<A> and
    Ptr<B>. I've got a strange feeling this isn't supposed to work.

    Oh, and the compiler produces "internal errors" if I try to repeat this
    trick in Ptr with other functions...

    Any ideas?
    Thomas
     
    Thomas Lorenz, Oct 15, 2004
    #1
    1. Advertising

  2. Thomas Lorenz wrote:
    > im working on a problem related to smart pointers. If the following is
    > legal C++
    >
    > A* a(new A());
    > B* b;
    > b = a;
    >
    > I want to be able to do the following:
    >
    > Ptr<A> a(new A());
    > Ptr<B> b;
    > b = a;
    >
    > with equivalent semantics. Eventually, I came up with the following
    > "solution", involving a strange friend declaration:
    >
    > template<class T>
    > class Ptr {
    > private:
    > T* m_pObject;
    > public:
    >
    > // Constructors etc. removed for simplicity
    >
    > template<class U>
    > inline friend void copy(Ptr<T>& to, Ptr<U>& from)
    > {
    > to.m_pObject = from.m_pObject;
    > };
    >
    > // Curious trick
    > template<class U>
    > friend void copy(Ptr<U>&, Ptr<T>&);
    >
    > template<class U>
    > inline Ptr(Ptr<U>& rhs)
    > : m_pObject(0)
    > {
    > copy(*this, rhs);
    > }


    I wonder what the need for this templatised c-tor is...

    >
    > template<class U>
    > inline Ptr<T> operator=(Ptr<U>& rhs)
    > {
    > copy(*this,rhs);
    > return *this;
    > }
    > }

    ;
    >
    > This works quite well using the MS 7.0 compiler. But I really don't know
    > why this "trick" enables copy() to access private members of Ptr<A> and
    > Ptr<B>. I've got a strange feeling this isn't supposed to work.


    Looks OK (at a glance).

    > Oh, and the compiler produces "internal errors" if I try to repeat this
    > trick in Ptr with other functions...
    >
    > Any ideas?


    Upgrade to 7.1. Post the "other functions" on which the compiler chokes.

    Victor
     
    Victor Bazarov, Oct 15, 2004
    #2
    1. Advertising

  3. Victor Bazarov <> wrote in
    news:LnQbd.4936$09.us.to.verio.net:

    >> This works quite well using the MS 7.0 compiler. But I really don't
    >> know why this "trick" enables copy() to access private members of
    >> Ptr<A> and Ptr<B>. I've got a strange feeling this isn't supposed to
    >> work.

    >
    > Looks OK (at a glance).


    Sure, and it works. But why?

    >> Oh, and the compiler produces "internal errors" if I try to repeat
    >> this trick in Ptr with other functions...
    >>
    >> Any ideas?

    >
    > Upgrade to 7.1. Post the "other functions" on which the compiler
    > chokes.
    >


    I tried adding equality operations with something like

    template<class U>
    inline bool equal(RefPtr<T>& lhs, RefPtr<U>& rhs) {
    return lhs.m_pObject == rhs.m_pObject;
    }

    template<class U>
    inline bool equal(RefPtr<U>& lhs, RefPtr<T>& rhs);

    Thomas
     
    Thomas Lorenz, Oct 18, 2004
    #3
    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. jblazi
    Replies:
    5
    Views:
    435
    jblazi
    Aug 16, 2004
  2. Horace Nunley

    why why why does function not work

    Horace Nunley, Sep 27, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    461
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Sep 27, 2006
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    903
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,966
    Smokey Grindel
    Dec 2, 2006
  5. avasilev
    Replies:
    4
    Views:
    578
    avasilev
    Dec 22, 2011
Loading...

Share This Page