pointers to overloaded member functions

Jonathan Hamaker hamaker@ISIP.MsState.EDU
Mon Oct 11 19:05:00 GMT 1999


Hi again Martin.

Though see the logic behind many of your conclusions, I must say that
I find the use of inheritance to be inconsistent. I find it odd that
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. 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. This might
make perfect sense according to the compiler, but I am not seeing the
distinction in the way that inheritance is being used. According to 
the way you outlined the below example I might expect the call to gt() 
above to be defined as a function taking a Char& and returning a bool, but
clearly, the compiler accepts the InheritChar& in place of the Char&
in this case. 

Maybe you can shed some light on my confusion. I have not tried this
case on any other C++ compiler, so I am afraid I do not have another
reference point.

Best regards and thank you again for your time,

Jon Hamaker
Institute for Signal and Information Processing
Mississippi State University
hamaker@isip.msstate.edu

> > Hi Martin, Thank you for your reply. My apologies for wasting your
> > time on the faulty test case before. Here is one that I have actually
> > tested and annotated with the errors that I consider to be against
> > standard C++ (perhaps I am misreading the standard).
> 
> Ok. I still think that the error is in your code, and that g++
> correctly analyses the error. See below.
> 
> [...]
> > class Char {
> [...]
> >   inline bool gt(Char& arg) {
> >     return (bool)(value_d > arg.value_d);
> >   }
> 
> I assume that you want this function to be selected later on in the
> first call to test_it, right? a function expecting Char&, and
> returning bool. Let's go on.

This is correct. 

> [...]
> > };
> 
> > class InheritChar : public Char {
> [...]
> > };
> 
> > template <class TObject>
> > class Test {
> [...]
> >   inline bool test_it(bool (TObject::*method_a)(TObject&)) {
> >   }
> 
> The test_it function expects a method of the class of the template
> parameter, returning bool, expecting a reference to
> template-parameter.
> 
> > };
> > 
> > int main() {
> >     // This section does not compile
> >      
> >   Test<InheritChar> test_class;
> 
> test_class is instantiated with InheritChar, i.e. TObject is
> InheritChar. That is, the signature of test_class.test_it is
> 
>   bool test_it(bool (InheritChar::*)(InheritChar &));
> 
> >   test_class.test_it(&InheritChar::gt);
> 
> InheritChar::gt is not redefined inside InheritChar, so it still has
> the signature
> 
>     bool gt(Char&);
> 
> Now, you cannot convert a method expecting some argument into a method
> expecting another argument. To convert a pointer-to-member-function
> from one type to another, the argument types and the return type must
> be identical - only the class which it is a member of may change.
> 
> So the code is invalid. Since gcc does not know which of the
> InheritChar::gt variants you meant, it cannot display the type of
> method pointer - hence it writes {unknown type}.
> 
> Since you seem to think the code is valid, please let me know which of
> the conclusions above are in error.
> 
> Hope this helps,
> Martin


More information about the Gcc-bugs mailing list