Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Template problem: Why does this work?

Reply
Thread Tools

Template problem: Why does this work?

 
 
Thomas Lorenz
Guest
Posts: n/a
 
      10-15-2004
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-15-2004
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
 
Reply With Quote
 
 
 
 
Thomas Lorenz
Guest
Posts: n/a
 
      10-18-2004
Victor Bazarov <(E-Mail Removed)> wrote in
news:LnQbd.4936$(E-Mail Removed)09.us.to.ve rio.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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
template template arguments: expected a class template, got `Component<T1, T2, T3> gary.bernstein@gmail.com C++ 1 06-08-2007 07:10 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Re: A Newbie Question about template template template tom_usenet C++ 0 07-24-2003 12:06 PM
Re: A Newbie Question about template template template Chris Theis C++ 2 07-24-2003 09:42 AM



Advertisments