By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,906 Members | 1,387 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,906 IT Pros & Developers. It's quick & easy.

pointer to member function

P: n/a
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
Nov 9 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a

From: "Imre Palik" <fi********************@automation.siemens.com>
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 <su****@msdc.hcltech.com>
Nov 9 '05 #2

P: n/a

"Imre Palik" <fi********************@automation.siemens.com> wrote in
message news:am************@automation.siemens.com...
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 <su****@msdc.hcltech.com>
Nov 9 '05 #3

P: n/a

"Sumit Rajan" <su*********@gmail.com> wrote in message
news:3t************@individual.net...

"Imre Palik" <fi********************@automation.siemens.com> wrote in
message news:am************@automation.siemens.com...
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 <su****@msdc.hcltech.com>
Nov 9 '05 #4

P: n/a
"Sumit Rajan" <su*********@gmail.com> writes:
"Sumit Rajan" <su*********@gmail.com> wrote in message
news:3t************@individual.net...

"Imre Palik" <fi********************@automation.siemens.com> wrote in
message news:am************@automation.siemens.com...
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
Nov 9 '05 #5

P: n/a
>
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
Nov 9 '05 #6

P: n/a
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
Nov 9 '05 #7

P: n/a
John Harrison <jo*************@hotmail.com> 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
Nov 10 '05 #8

P: n/a
>
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
Nov 10 '05 #9

P: n/a
John Harrison <jo*************@hotmail.com> 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
Nov 11 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.