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