473,320 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
9 8814

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

"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

"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
"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
>
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
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
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
>
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: David Hill | last post by:
Hello - I am using a library that takes a function pointer as an argument. Is the code below not possible? int library_func(void (*func)(int, short, void *)); I am trying to do this... ...
5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
7
by: jon wayne | last post by:
Hi I'm a little confused here about the lifetime of a static pointer to member function, Say, I declare,define & initialize a static ptr to mem function in the header file of a class(the class...
6
by: keepyourstupidspam | last post by:
Hi, I want to pass a function pointer that is a class member. This is the fn I want to pass the function pointer into: int Scheduler::Add(const unsigned long timeout, void* pFunction, void*...
7
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
5
by: Tim Frink | last post by:
Hi, I'm experimenting with function pointers and found two questions. Let's assume this code: 1 #include <iostream> 2 class A; 3 4 //////////////////////////////////////////// 5 class B
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
4
by: Immortal_Nephi | last post by:
I had a lot of research to see how function pointer works. Sometimes, programmers choose switch keyword and function in each case block can be called. Sometimes, they choose ordinary function...
7
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.