Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > pointer to member function

Reply
Thread Tools

pointer to member function

 
 
Imre Palik
Guest
Posts: n/a
 
      11-09-2005
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
 
Reply With Quote
 
 
 
 
Sumit Rajan
Guest
Posts: n/a
 
      11-09-2005

From: "Imre Palik" <(E-Mail Removed)>
Newsgroups: comp.lang.c++
Sent: Thursday, November 10, 2005 12:16 AM
Subject: pointer to member function


> 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


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

Regards,
Sumit.
--
Sumit Rajan <(E-Mail Removed)>


 
Reply With Quote
 
 
 
 
Sumit Rajan
Guest
Posts: n/a
 
      11-09-2005

"Imre Palik" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
> 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'



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 <(E-Mail Removed)>


 
Reply With Quote
 
Sumit Rajan
Guest
Posts: n/a
 
      11-09-2005

"Sumit Rajan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "Imre Palik" <(E-Mail Removed)> wrote in
> message news:(E-Mail Removed)...
> However,
> A::B::B():mptr(&A::do_something){}
>
>
> compiles with Comeau C++ and with VC++.



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

Regards,
Sumit.
--
Sumit Rajan <(E-Mail Removed)>


 
Reply With Quote
 
Imre Palik
Guest
Posts: n/a
 
      11-09-2005
"Sumit Rajan" <(E-Mail Removed)> writes:

> "Sumit Rajan" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >
> > "Imre Palik" <(E-Mail Removed)> wrote in
> > message news:(E-Mail Removed)...
> > However,
> > A::B::B():mptr(&A::do_something){}
> >
> >
> > compiles with Comeau C++ and with VC++.

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


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
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      11-09-2005
>
> 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?
>


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
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      11-09-2005
Imre Palik wrote:
> ...
> 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?
> ...


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
 
Reply With Quote
 
Imre Palik
Guest
Posts: n/a
 
      11-10-2005
John Harrison <(E-Mail Removed)> writes:

> >
> > 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?
> >

>
> 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.


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

Thx

ImRe
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      11-10-2005
>
> Could you point me to the relevant part of the standard?
> I tried quite hard, but I can't find it.
>


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
 
Reply With Quote
 
Imre Palik
Guest
Posts: n/a
 
      11-11-2005
John Harrison <(E-Mail Removed)> writes:

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

>
> 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.


Thanks

ImRe
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer-to-member-function pointing to a member function of an inherited class akiriwas@gmail.com C++ 12 02-11-2005 05:15 PM
pointer to member function and pointer to constant member function Fraser Ross C++ 4 08-14-2004 06:00 PM
Function pointer member variable to non-member function Alex C++ 0 10-15-2003 05:26 PM
Function pointer member variable to non-member function slide_o_mix C++ 0 10-15-2003 03:37 PM
Passing a pointer to member function as a parameter to another member function Newsgroup - Ann C++ 5 07-30-2003 02:54 AM



Advertisments