From 4588793158755748881
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: Michael Hudson <sorry.no.email@nowhere.com>
Subject: Re: my own 'pointer type'
Date: 1997/03/07
Message-ID: <5fperm$nh3@netlab.cs.rpi.edu>#1/1
X-Deja-AN: 223806430
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, 06 Mar 1997 16:44:02 +0000
Organization: University of Leicester, UK
Reply-To: none@leicester.ac.uk
Newsgroups: comp.lang.c++.moderated,comp.std.c++


Enno Rehling wrote:
> 
> Just checking:
> 
> 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.
> 

What you describe will (I think) work, but there's a better way.

template <class T>
class MagicPtr {
T *pointee;
...
template<classU>
operator MagicPtr<U> () { return MagicPtr<U>(pointee); }
...
};

This will make MagicPtr<T> almost as convertible as T*.

For a full discussion of smart pointers (for that is what these things 
are called) read More Effective C++ by Scott Meyers.

-- 
Regards,
    Michael Hudson

Please don't email this address - it's not mine.

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




