Connecting Tech Pros Worldwide Forums | Help | Site Map

pointer to member function

Imre Palik
Guest
 
Posts: n/a
#1: Nov 9 '05
Hi'yal,

consider this code:

class A
{
class B
{
void (A::*mptr)();
public:
B();
};

void do_something();
};

void
A::do_something() {}

A::B::B() : mptr(do_something) {} // error here

trying to compile it with gcc gives the following error:

m.cpp:17: error: argument of type `void (A::)()' does not match `void (A::*)()'

I fail to see the difference between these two types. Anybody cares to
explain? Changing the problematic line to

A::B::B() : mptr(&do_something) {} // error here

I get the following error message:

m.cpp:17: error: ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say `&A::do_something'

I cannot find the relevant part of the standard. Could somebody point me
to it?

Thx

ImRe

Sumit Rajan
Guest
 
Posts: n/a
#2: Nov 9 '05

re: pointer to member function



From: "Imre Palik" <firstname.lastname.ext@automation.siemens.com>
Newsgroups: comp.lang.c++
Sent: Thursday, November 10, 2005 12:16 AM
Subject: pointer to member function

[color=blue]
> Hi'yal,
>
> consider this code:
>
> class A
> {
> class B
> {
> void (A::*mptr)();
> public:
> B();
> };
>
> void do_something();
> };
>
> void
> A::do_something() {}
>
> A::B::B() : mptr(do_something) {} // error here[/color]

You could try replacing the above line with:
A::B::B():mptr(&A::do_something){}

Regards,
Sumit.
--
Sumit Rajan <sumitr@msdc.hcltech.com>


Sumit Rajan
Guest
 
Posts: n/a
#3: Nov 9 '05

re: pointer to member function



"Imre Palik" <firstname.lastname.ext@automation.siemens.com> wrote in
message news:amslu5d5hq.fsf@automation.siemens.com...[color=blue]
> Hi'yal,
>
> consider this code:
>
> class A
> {
> class B
> {
> void (A::*mptr)();
> public:
> B();
> };
>
> void do_something();
> };
>
> void
> A::do_something() {}
>
> A::B::B() : mptr(do_something) {} // error here
>
> trying to compile it with gcc gives the following error:
>
> m.cpp:17: error: argument of type `void (A::)()' does not match `void
> (A::*)()'
>
> I fail to see the difference between these two types. Anybody cares to
> explain? Changing the problematic line to
>
> A::B::B() : mptr(&do_something) {} // error here
>
> I get the following error message:
>
> m.cpp:17: error: ISO C++ forbids taking the address of a bound member
> function to form a pointer to member function. Say `&A::do_something'[/color]


Sorry. I did not read this part before my previous post.

However,
A::B::B():mptr(&A::do_something){}


compiles with Comeau C++ and with VC++.

Regards,
Sumit.
--
Sumit Rajan <sumitr@msdc.hcltech.com>


Sumit Rajan
Guest
 
Posts: n/a
#4: Nov 9 '05

re: pointer to member function



"Sumit Rajan" <sumit.rajan@gmail.com> wrote in message
news:3tevn2Fsk2dfU1@individual.net...[color=blue]
>
> "Imre Palik" <firstname.lastname.ext@automation.siemens.com> wrote in
> message news:amslu5d5hq.fsf@automation.siemens.com...
> However,
> A::B::B():mptr(&A::do_something){}
>
>
> compiles with Comeau C++ and with VC++.[/color]


And with g++ 3.4.2. Which version are you using?

Regards,
Sumit.
--
Sumit Rajan <sumitr@msdc.hcltech.com>


Imre Palik
Guest
 
Posts: n/a
#5: Nov 9 '05

re: pointer to member function


"Sumit Rajan" <sumit.rajan@gmail.com> writes:
[color=blue]
> "Sumit Rajan" <sumit.rajan@gmail.com> wrote in message
> news:3tevn2Fsk2dfU1@individual.net...[color=green]
> >
> > "Imre Palik" <firstname.lastname.ext@automation.siemens.com> wrote in
> > message news:amslu5d5hq.fsf@automation.siemens.com...
> > However,
> > A::B::B():mptr(&A::do_something){}
> >
> >
> > compiles with Comeau C++ and with VC++.[/color]
>
>
> And with g++ 3.4.2. Which version are you using?[/color]

I know that it compiles. I just want to know why doesn't it compile
without the A:: part. AFAIK do_something() should be in scope in a method
of an embedded class. Or am I missing something?

ImRe
John Harrison
Guest
 
Posts: n/a
#6: Nov 9 '05

re: pointer to member function


>[color=blue]
> I know that it compiles. I just want to know why doesn't it compile
> without the A:: part. AFAIK do_something() should be in scope in a method
> of an embedded class. Or am I missing something?
>[/color]

Yes, the syntax of C++. The expression &T::f is a pointer to member. No
other syntax will do, not T::f or &(T::f) or just plain f. Scope is
irrelevant. Just one of those things.

john
Andrey Tarasevich
Guest
 
Posts: n/a
#7: Nov 9 '05

re: pointer to member function


Imre Palik wrote:[color=blue]
> ...
> I know that it compiles. I just want to know why doesn't it compile
> without the A:: part. AFAIK do_something() should be in scope in a method
> of an embedded class. Or am I missing something?
> ...[/color]

Scope is completely irrelevant here. In C++ the one and only way to obtain a
pointer to member is to use '&' operator explicitly and to specify a qualified
name of the member: '&A::do_something'.

Neither 'do_something' nor 'A::do_something' is the correct way to do it.

--
Best regards,
Andrey Tarasevich
Imre Palik
Guest
 
Posts: n/a
#8: Nov 10 '05

re: pointer to member function


John Harrison <john_andronicus@hotmail.com> writes:
[color=blue][color=green]
> >
> > I know that it compiles. I just want to know why doesn't it compile without the A::
> > part. AFAIK do_something() should be in scope in a method
> > of an embedded class. Or am I missing something?
> >[/color]
>
> Yes, the syntax of C++. The expression &T::f is a pointer to member. No other syntax will
> do, not T::f or &(T::f) or just plain f. Scope is irrelevant. Just one of those things.[/color]

Could you point me to the relevant part of the standard?
I tried quite hard, but I can't find it.

Thx

ImRe
John Harrison
Guest
 
Posts: n/a
#9: Nov 10 '05

re: pointer to member function


>[color=blue]
> Could you point me to the relevant part of the standard?
> I tried quite hard, but I can't find it.
>[/color]

I know the feeling. It's 5.3.1 para 3. The important part is the mention
of 'qualified-id', i.e. you must include the class name.

john
Imre Palik
Guest
 
Posts: n/a
#10: Nov 11 '05

re: pointer to member function


John Harrison <john_andronicus@hotmail.com> writes:
[color=blue][color=green]
> >
> > Could you point me to the relevant part of the standard?
> > I tried quite hard, but I can't find it.
> >[/color]
>
> I know the feeling. It's 5.3.1 para 3. The important part is the mention of
> 'qualified-id', i.e. you must include the class name.[/color]

Thanks

ImRe
Closed Thread