pointer to member function

I

Imre Palik

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
 
S

Sumit Rajan

From: "Imre Palik" <[email protected]>
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.
 
S

Sumit Rajan

Imre Palik said:
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.
 
S

Sumit Rajan

Sumit Rajan said:
message 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.
 
I

Imre Palik

Sumit Rajan said:
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
 
J

John Harrison

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
 
A

Andrey Tarasevich

Imre said:
...
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.
 
I

Imre Palik

John Harrison said:
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
 
J

John Harrison

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
 
I

Imre Palik

John Harrison said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top