Topic: Other kinds of generic pointers
Author: bill@gibbons.org (Bill Gibbons)
Date: 2000/08/10 Raw View
In article <398F4203.A654D40C@tribble.com>, David R Tribble
<david@tribble.com> wrote:
> Coming up with a generic member function pointer type is more
> difficult (since C++ doesn't have a global 'Object' class shared by
> all other classes).
No, but any class will do:
class GenericPMClass { };
typedef void (GenericPMClass::*GenericPMF)();
and for generic pointers to data members:
typedef int GenericPMClass::*GenericPDM;
The standard specifically allows you to cast any pointer to member to
an unrelated pointer to member type (as long as both are pointers to
member functions or pointers to data members) and back, and guarantees
that the result will be the original pointer to member. (See section
5.2.10 paragraph 9.)
This was added specifically to let you write generic pointers to members.
(However, as I recall, VC++ deliberately violates the standard in the area
of pointers to members and some care must be taken to ensure that this
works. It may be necessary for GenericPMClass to have a virtual function
and two base classes to force its pointer to member representation into
a sufficiently general form. This is not necessary with a standard
conforming compiler. Such code would still work with other compilers
and is relatively harmless since no objects of this type are ever created.)
-- Bill Gibbons
bill@gibbons.org
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: David R Tribble <david@tribble.com>
Date: 2000/08/08 Raw View
Daryle Walker <dwalker07@snet.net.invalid> writes
>> I heard that void* was not originally part of C, put it was added
>> later (and carried to C++) when the need for a generic data pointer
>> arose.
>> Why isn't there corresponding generic function or member pointers?
>> They would be needed like when void* is used, when we have the
>> address but not (at the time) the exact form.
This is a valid question because casting between pointers-to-data,
pointers-to-functions, and pointers-to-member-functions are undefined
operations. Thus storing a function pointer in a "generic" pointer
of type 'void*' won't work. Hence the need for a generic function
pointer and a generic member function pointer type.
Walker:
>> I think we would have
>> to create two new keywords for these types, though.
Not necessarily. We would just have to create a certain syntax for
these types, probably based around the existing keyword 'void'.
(See below.)
Francis Glassborow wrote:
> what is wrong with:
>
> typedef void (*genericfunction)();
>
> Of course you will have use a cast to use it, but that is no bad thing
> in my book.
I would prefer a slightly different type:
typedef void (*vfp_t)(void);
(The name of the type is unimportant.)
Coming up with a generic member function pointer type is more
difficult (since C++ doesn't have a global 'Object' class shared by
all other classes).
--
David R. Tribble, mailto:david@tribble.com, http://david.tribble.com
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 2000/08/08 Raw View
David R Tribble wrote:
> I would prefer a slightly different type:
>
> typedef void (*vfp_t)(void);
You have just repeated the same thing.
--
Valentin Bonnard
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: David R Tribble <david@tribble.com>
Date: 2000/08/09 Raw View
[Generic function pointer type]
Francis Glassborow wrote:
>> what is wrong with:
>> typedef void (*genericfunction)();
David Tribble wrote:
> I would prefer a slightly different type:
> typedef void (*vfp_t)(void);
Valentin Bonnard wrote:
> You have just repeated the same thing.
Bouncing back and forth between news:comp.std.c and news:comp.std.c++
can make one forget the differences between C and C++!
Still, my typedef has the advantage that it could be used in both
C and C++.
--
David R. Tribble, mailto:david@tribble.com, http://david.tribble.com
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 2000/08/04 Raw View
In article <1eerm31.1s8xxzjej2ye6N%dwalker07@snet.net.invalid>, Daryle
Walker <dwalker07@snet.net.invalid> writes
>I heard that void* was not originally part of C, put it was added later
>(and carried to C++) when the need for a generic data pointer arose.
>Why isn't there corresponding generic function or member pointers? They
>would be needed like when void* is used, when we have the address but
>not (at the time) the exact form. I think we would have to create two
>new keywords for these types, though.
what is wrong with:
typedef void (*genericfunction)();
Of course you will have use a cast to use it, but that is no bad thing
in my book.
Francis Glassborow Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 2000/08/05 Raw View
Francis Glassborow wrote:
> In article <1eerm31.1s8xxzjej2ye6N%dwalker07@snet.net.invalid>, Daryle
> Walker <dwalker07@snet.net.invalid> writes
> >I heard that void* was not originally part of C, put it was added later
> >(and carried to C++) when the need for a generic data pointer arose.
> >Why isn't there corresponding generic function or member pointers?
Because C and C++ aren't orthogonal and clean languages. The need for a
generic data type is very high in C, the need for a generic pointer type
is not, or wasn't felt as such by the C committee.
It's more a C question than a C++ question because in C++ we prefer safe
techniques to untyped pointers and casts.
> >They
> >would be needed like when void* is used, when we have the address but
> >not (at the time) the exact form. I think we would have to create two
> >new keywords for these types, though.
>
> what is wrong with:
>
> typedef void (*genericfunction)();
>
> Of course you will have use a cast to use it, but that is no bad thing
> in my book.
What's wrong is that convertions to genericfunction won't be implicit
as they should be, and convertions back won't be doable with a
static_cast
as they should be.
--
Valentin Bonnard
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 2000/08/06 Raw View
In article <398B8369.76B7@wanadoo.fr>, Valentin Bonnard
<Bonnard.V@wanadoo.fr> writes
>What's wrong is that convertions to genericfunction won't be implicit
>as they should be, and convertions back won't be doable with a
>static_cast
>as they should be.
The 'should be' is a matter of opinion. I have grave doubts about the
advantages of having any implicit casting between pointer types.
Francis Glassborow Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: kanze@gabi-soft.de
Date: 2000/08/07 Raw View
Francis Glassborow <francis@robinton.demon.co.uk> writes:
|> In article <398B8369.76B7@wanadoo.fr>, Valentin Bonnard
|> <Bonnard.V@wanadoo.fr> writes
|> >What's wrong is that convertions to genericfunction won't be implici=
t=20
|> >as they should be, and convertions back won't be doable with a
|> >static_cast=20
|> >as they should be.
|> The 'should be' is a matter of opinion. I have grave doubts about th=
e
|> advantages of having any implicit casting between pointer types.
Even Derived* to Base*?
--=20
James Kanze mailto:kanze@gabi-soft.de
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
Ziegelh=FCttenweg 17a, 60598 Frankfurt, Germany Tel. +49(069)63198627
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: dwalker07@snet.net.invalid (Daryle Walker)
Date: 2000/08/03 Raw View
I heard that void* was not originally part of C, put it was added later
(and carried to C++) when the need for a generic data pointer arose.
Why isn't there corresponding generic function or member pointers? They
would be needed like when void* is used, when we have the address but
not (at the time) the exact form. I think we would have to create two
new keywords for these types, though.
--
Daryle Walker
Mac, Internet, and Video Game Junkie
dwalker07 AT snet DOT net
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]