Topic: my own 'pointer type
Author: "Enno Rehling" <enno@uni-paderborn.de>
Date: 1997/03/05 Raw View
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.
My new solution is shown below. The question I have is: This looks kind of
funny to me, so I'd like to know
a) is this okay with the current C++ standard?
b) which compilers will accept it? I know my Visual C++ 4.0 does accept it,
but I know it's not the most conforming compiler in the world, and g++, Sun
and SGI are platforms I'm targeting as well.
Anyway, here's the code. If you could answer one of the two questions
above, I'd be eternally happy (especially if the answer is 'yes').
----- BEGIN CODE -------
template <class T>
struct ptr : public ptr<T::base_type>
{
ptr(unsigned int ui, T* p) : ptr<T::base_type>(ui, p) {}
};
struct ptr<void>
{
unsigned int id;
void* data;
ptr(unsigned int ui, void* p) : id(ui), data(p) {}
};
struct A
{
typedef void base_type;
};
struct B : public A
{
typedef A base_type;
};
int main()
{
B b;
ptr<B> p(42, &b);
return p.id;
}
----- END CODE -----
Enno Rehling Kilianstra_e 129a 33098 Paderborn Germany
rehling@usa.net http://hrz.uni-paderborn.de/~q09960/public/
Tel/Fax: Int+ 49 (0) 5251-760796
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
Author: Hyman Rosen <jyacc!hymie@uunet.uu.net>
Date: 1997/03/07 Raw View
"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! ]
Author: Michael Hudson <sorry.no.email@nowhere.com>
Date: 1997/03/07 Raw View
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! ]
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1997/03/08 Raw View
Hyman Rosen writes:
> "Enno Rehling" <enno@uni-paderborn.de> writes:
>> 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:
I don't think conversion operators would do it as well as inheritance.
In fact, there are two cases I can think of where such a construction
would not do it well: when an additional user-defined conversion would
be necessary and in cases that presented covariance.
In fact, I'd love if auto_ptr were declared this way, so that
covariance would work with auto_ptr's too:
template <class T, class Base = void>
class auto_ptr : auto_ptr<Base> {
// implementation that just prevents pointers to different types
// from being assigned
};
template <class T>
class auto_ptr<T, void> {
// actual implementation
};
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
Universidade Estadual de Campinas, SP, Brasil
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1997/03/08 Raw View
Hyman Rosen writes:
> "Enno Rehling" <enno@uni-paderborn.de> writes:
>> 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:
I don't think conversion operators would do it as well as inheritance.
In fact, there are two cases I can think of where such a construction
would not do it well: when an additional user-defined conversion would
be necessary and in cases that presented covariance.
In fact, I'd love if auto_ptr were declared this way, so that
covariance would work with auto_ptr's too:
template <class T, class Base = void>
class auto_ptr : auto_ptr<Base> {
// implementation that just prevents pointers to different types
// from being assigned
};
template <class T>
class auto_ptr<T, void> {
// actual implementation
};
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
Universidade Estadual de Campinas, SP, Brasil
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
---
[ comp.std.c++ is moderated. To submit articles: try just posting with
]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu
]
[ FAQ:
http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy:
http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu
]
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]