This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: pointers to overloaded member functions
- To: hamaker at ISIP dot MsState dot EDU
- Subject: Re: pointers to overloaded member functions
- From: "Martin v. Loewis" <martin at mira dot isdn dot cs dot tu-berlin dot de>
- Date: Tue, 12 Oct 1999 09:45:26 +0200
- CC: gnu-gcc-bug at gnu dot org
- References: <199910120205.VAA24360@isip22.isip.msstate.edu>
> Though see the logic behind many of your conclusions, I must say that
> I find the use of inheritance to be inconsistent.
Jon,
I can understand your confusion. Please trust me that this is the way
in which C++ is defined, and this is how any good C++ compiler should
behave.
There are technical reasons why it works that way, which I'll try to
explain in a moment. The C++ language was defined both from the
viewpoint of ease-of-use, as well as implementability (what good is a
language you can't write a compiler/interpreter for). If you are still
not satisfied, please ask your questions in comp.lang.c++.moderated,
which is the appropriate forum for C++ discussions.
> this works:
>
> InheritChar ichar;
> ichar.assign(L'a');
> Char rchar(L'b');
>
> if (rchar.gt(ichar)) {
> printf("b greater than a\n");
> }
>
> because the InheritChar in the call to gt() is implicitly cast (is
> that the right term?) to an object of type Char - i.e. inheritence
> works.
Yes. Please note that in C++, the implicit cast may involve adjustment
of the address of an object (i.e. the internal representation of the
reference may change), which happens in case of virtual or multiple
inheritance.
> However, when I try to do what I outlined below - use a pointer to a
> method where the method takes an argument of the type of the base
> class, I get an error because there is no implicit casting.
Suppose you were later going to use the pointer-to-member:
inline bool test_it(bool (InheritChar::*method_a)(InheritChar&)) {
local_obj_d.assign(L'a');
InheritChar test_obj(L'b');
bool grthan = (local_obj_d.*method_a)(test_obj);
(replacing the template parameter to simplify the example). Since the
argument to method_a (i.e. test_obj) is an InheritChar, and since the
method_a expects an InheritChar, no reference adjustment would be
performed by the compiler. However, if method_a is indeed
Char::gt(Char&), we saw that an adjustment might be necessary. As a
result, the code would crash.
Please note that there is no way for the compiler to know, at
run-time, whether the method stored in method_a requires adjustments
to it arguments before being invoked. To support this, a
pointer-to-method would have to store, dynamically, type information
for all its arguments. This is impossible to achieve, when you want a
fixed-size representation for pointers tomember functions.
Regards,
Martin