473,472 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Template problem: Why does this work?

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

Jul 22 '05 #1
2 1081
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
Jul 22 '05 #2
Victor Bazarov <v.********@comAcast.net> wrote in
news:Ln***************@newsread1.dllstx09.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
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: nikola | last post by:
Hi all, I was working with a simple function template to find the min of two values. But since I would like the two values to be different (type) I dont know what kind of value (type) it will...
7
by: Drew McCormack | last post by:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this: template <typename T> class M { static Registrar<M>...
0
by: Chris F Clark | last post by:
In our C++ project we have some internal bug reporting macros that we use to get useful information when the program does something unexpected. Essentially at the point of the error, we invoke an...
11
by: gao_bolin | last post by:
I am facing the following scenario: I have a class 'A', that implements some concept C -- but we know this, not because A inherits from a virtual class 'C', but only because a trait tell us so: ...
3
by: Chris | last post by:
I am having a very strange problem involving virtual functions in template classes. First of all, here is an extremely simplified structure of the two classes I am having problems with. ...
4
by: Brian Barnes | last post by:
I am trying to create a template code behind file for a website which all pages should be inherited from. This base class however, when I try to view the aspx page in Visual studio .NET (2002), it...
6
by: Hendrik Schober | last post by:
Hi, I have a problem with extending some existing code. In a simplified form, the problem looks like this: I have four types, A, B, C, and D. Each A refers to zero, one, or more B's and each...
6
by: Neal | last post by:
Hi All, I used an article on XSLT and XML and creating a TOC written on the MSDN CodeCorner. ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/dncodecorn/html/corner042699.htm However, it did'nt...
8
by: Fab | last post by:
All, I need your help understanding why the following code does *NOT* compile with G++ (tested with gcc 3.x and 4.1.x): ---------------------------------------------------------------------...
2
by: Clyde | last post by:
Hi, what i'm trying to do is: /////////////// Code Start template <class TType, int* p = 0> class Template { public:
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.