From -1063365396674196045
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: fc772,b03957313f1e9325
X-Google-Attributes: gidfc772,public
X-Google-Thread: f78e5,b03957313f1e9325
X-Google-Attributes: gidf78e5,public
From: Hyman Rosen <jyacc!hymie@uunet.uu.net>
Subject: Re: my own 'pointer type'
Date: 1997/03/07
Message-ID: <5fokhq$j74@netlab.cs.rpi.edu>#1/1
X-Deja-AN: 223734269
Sender: cppmods@netlab.cs.rpi.edu
References: <5fjlv7$jjl@netlab.cs.rpi.edu>
X-Submission-Address: c++-submit@netlab.cs.rpi.edu
X-Original-Date: Thu, 6 Mar 1997 15:38:00 GMT
Organization: JYACC, Inc.
Newsgroups: comp.lang.c++.moderated,comp.std.c++


"Enno Rehling" <enno@uni-paderborn.de> writes:

> Here's what I want to do: I want a kind of 'pointer type' that would allow
> me to have something like a pointer to an object, plus additional info (an
> id, for example). I figured that templates would be neat, I'd write ptr<A>
> a; and be able to access a.id, and overload the -> operator to access the
> contents of A. Worked fine, up to a point: typecasts.
> At one point, I had a class B derived from A, and it dawned on me that
> ptr<B> does not conform to ptr<A>, because it's not derived from it.

There's a better way than the one you used, but it requires
support for member templates:

template <typename T>
struct ptr
{
	int id;
	T *p;
	ptr(int ID, T *P) : id(ID), p(P) { }
	template <typename U> operator ptr<U>() { return ptr<U>(id, p); }
};

Note the templated typecast member function - it will succeed only
for those cases where a U* can be initialized from a T*, which will
give you exactly the behavior you want. Now all you need to do is
find a compiler which supports this.

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]




