473,378 Members | 1,451 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,378 software developers and data experts.

Pointer to Member Functions (is "&" required?)

When I was learning C, I learned that the data type of a function name
by itself is a function pointer. For example

int
someFunction(char* str)
{
....
}
....
int (*fPtr) (char *) = someFunction;
....

I was, therefore, extremely surprised (while reading about pointers to
non-static member functions) when I saw that in C++, you need to
supply the address operator in front of the function name. That is,
the above statement will be something like this:

int (*fPtr) (char *) = &someFunction;
To explain what I am saying, let me take the liberty to include a
small code snippet from "C++ FAQs Second Edition" by Marshall Cline et
al.

FAQ 38.04 (How is an array of pointers to non static member functions
declared?)

class Fred {
public:
void f(int i);
void g(int i);
void h(int i);
};

....
typedef void (Fred::*FredMemberPtr)(int);
....

FredMemberPtr array[3] = {&Fred::f, &Fred::g, &Fred::h};
....
According to me, the above statement should have been something like
this:
FredMemberPtr array[3] = {Fred::f, Fred::g, Fred::h};
Why is the address operator (&) needed in this case at all? Kindly
explain. Is there a difference between C and C++ in this respect?

Thanks,
Nimmi
Jul 19 '05 #1
5 2659
In article <8b*************************@posting.google.com> ,
ni*************@yahoo.com says...
When I was learning C, I learned that the data type of a function name
by itself is a function pointer.
That's correct.

[ ... ]
I was, therefore, extremely surprised (while reading about pointers to
non-static member functions) when I saw that in C++, you need to
supply the address operator in front of the function name.
That's also correct -- basically put, Bjarne screwed up, and the
committee didn't fix his mistake. AFAICT, Bjarne's reasoning was that
since he apparently normally used the '&' when taking the address of a
normal function, that he should require it when taking the address of a
member function. Therefore, in C++ taking the address of a member
function _requires_ the '&' in front of the name, even though it would
be trivial to specify the language so it wasn't required, and I don't
believe the ampersand makes parsing easier either.

[ ... ]
Why is the address operator (&) needed in this case at all?
Because the standard says it is. There's no other reason.
Kindly
explain. Is there a difference between C and C++ in this respect?


Yes, there are a number of differences. One is that C doesn't have
member functions.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #2
Jerry Coffin wrote:
I was, therefore, extremely surprised (while reading about pointers to
non-static member functions) when I saw that in C++, you need to
supply the address operator in front of the function name.

That's also correct


But only for member functions, right? The address-of operator is still
optional for non-member functions, isn't it?

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #3
Kevin Goodsell wrote:
Jerry Coffin wrote:
I was, therefore, extremely surprised (while reading about pointers to
non-static member functions) when I saw that in C++, you need to
supply the address operator in front of the function name.


That's also correct

But only for member functions, right? The address-of operator is still
optional for non-member functions, isn't it?


Yep - I think you're right. Only member functions require operator & to
get the address.

A piece of opinion, I have no problem with requiring the address of
operator for member functions. I think code written without it are a
little more difficult to read, it is however unfortunate that there is a
inconsistantcy for not needing the & operator on non-member functions
but if anyone cares, they have the option of using the & operator on
those as well.

Jul 19 '05 #4
On 21 Sep 2003 08:34:57 -0700, ni*************@yahoo.com (Nimmi
Srivastav) wrote in comp.lang.c++:
When I was learning C, I learned that the data type of a function name
by itself is a function pointer. For example

int
someFunction(char* str)
{
...
}
...
int (*fPtr) (char *) = someFunction;
...
The same syntax produces the identical results in C++

I was, therefore, extremely surprised (while reading about pointers to
non-static member functions) when I saw that in C++, you need to
supply the address operator in front of the function name. That is,
the above statement will be something like this:

int (*fPtr) (char *) = &someFunction;
To explain what I am saying, let me take the liberty to include a
small code snippet from "C++ FAQs Second Edition" by Marshall Cline et
al.

FAQ 38.04 (How is an array of pointers to non static member functions
declared?)

class Fred {
public:
void f(int i);
void g(int i);
void h(int i);
};

...
typedef void (Fred::*FredMemberPtr)(int);
...

FredMemberPtr array[3] = {&Fred::f, &Fred::g, &Fred::h};
...
According to me, the above statement should have been something like
this:
FredMemberPtr array[3] = {Fred::f, Fred::g, Fred::h};
Why should we care what it should be "according to you"? We have ISO
international standards so we do not have to depend on people's
opinions, even yours.
Why is the address operator (&) needed in this case at all? Kindly
explain. Is there a difference between C and C++ in this respect?
Yes, there is a difference between C and C++ in this respect. C++
requires use of the address-of operator ("&") to take the address of a
non-static member function. C, on the other hand, does not have
member functions, either static or non-static. So it does not define
the syntax of taking the address of something that does not exist in
the language.
Thanks,
Nimmi


Perhaps Bjarne's intent was to emphasize the difference between member
functions and free-standing, C compatible functions. To keep
programmers from forgetting the significant differences between the
two, or the fact that they are not compatible with each other nor
convertible to each other.

Or perhaps it was merely a style preference on Dr. Stroustrup's part,
as one other reply suggests.

The use of the ampersand is required by the C++ standard. The reason
that this requirement is part of the standard is irrelevant in this
group. You could purchase Stroustrup's book "Design and Evolution of
C++", although off-hand I can't remember what it says about this.

If you think this matter is such a serious handicap to writing correct
C++ programs that it should be changed in a future version of the C++
standard, post your comment to comp.std.c++.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Jul 19 '05 #5
In article <0Q***************@newsread3.news.pas.earthlink.ne t>,
us*********************@neverbox.com says...

[ requirement to use '&' to take the address of a function ]
But only for member functions, right? The address-of operator is still
optional for non-member functions, isn't it?


Yes, that's correct.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: CoolPint | last post by:
Can anyone clearly explain the difference between constant reference to pointers and reference to constant pointers? What is const int * & ? Is it a constant reference to a pointer to an...
4
by: Jian H. Li | last post by:
Hello, What's the essential differences between the two ways of "class::member" & "object.member"(or object_pointer->member)? class C{ public: void f() {} int i; };
6
by: Otto Wyss | last post by:
I've the following function declaration: wxTree GetLastChild (const wxTree& item, long& cookie) const; I'd like to make the cookie parameter optional, i.e. "long& cookie = ....", without...
4
by: barney | last post by:
Hello, I' m using .NET System.Xml.XmlDOcument. When I do the following: XmlDocument xml = new XmlDocument(); xml.Load("blah"); .... xml.Save("blub"); I've got the problem that the following...
5
by: martin | last post by:
Hi, I would be extremly grateful for some help on producing an xml fragemt. The fragment that I wish to produce should look like this <Addresses> <Address>&qout;Somebody's Name&quot;...
4
by: Peter Oliphant | last post by:
I've upgraded my code to VS C++.NET 2005 (Express) using /clr pure. My question is, why is there an exception to the rule in how pointer syntax is done in the event handlers? For example, why is...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
6
by: active | last post by:
This works with Strict Off But not with Strict On Sometimes I can figure out what is needed by running it and using QuickWatch to see the type required but GetObject("winmgmts:\\" &...
30
by: Medvedev | last post by:
i see serveral source codes , and i found they almost only use "new" and "delete" keywords to make they object. Why should i do that , and as i know the object is going to be destroy by itself at...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.