Pointer to const member functions

Philippe Bouchard philippeb@corel.com
Tue Nov 14 11:45:00 GMT 2000


The following program:
#include <iostream>
using namespace std;
struct A
{
  int a;
  static void function1(int)
  {
    cout << __PRETTY_FUNCTION__ << endl;
  }
  void function2(int)
  {
    this->a = 0;
    cout << __PRETTY_FUNCTION__ << endl;
  }
  void (* const fp1())(int) const
  {
    this->a = 0;    // Works, OK
    cout << __PRETTY_FUNCTION__ << endl;
    return & A::function1;
  }
  void (A::* const fp2() const)(int) const
  {
    //this->a = 0;    // Won't work,
OK
    cout << __PRETTY_FUNCTION__ << endl;
    return & A::function2;
  }
};
int main()
{
  A a;
  a.fp1()(0);
  (a.*a.fp2())(0);
}
Outputs:
void (*const A::fp1())(int) const
static void A::function1(int)
void (A::* A::fp2())(int) const const
void A::function2(int)
Under:
Reading specs from /usr/lib/gcc-lib/i386-linux/2.95.2/specs
gcc version 2.95.2 20000220 (Debian GNU/Linux)
What I think is wrong, is that:
1- fp1() returns a pointer to a function. Why can I write 'const' at
the very end of the line? It's not a pointer to a member.
2- fp2() returns a pointer to a const member function. That particular
member happens to be A::function2 which is not a constant member. Is the
'const' silently discarded here?
Thank you.
begin:vcard 
n:Bouchard;Philippe
x-mozilla-html:FALSE
org:Corel Linux
adr:;;;;;;
version:2.1
email;internet:philippeb@corel.com
title:Software Engineer
x-mozilla-cpt:;0
fn:Philippe Bouchard
end:vcard


More information about the Gcc-bugs mailing list