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

Use of & operator for function pointers

Hi,

I was curious whether or not to use the & operator when creating a
function pointer. It seems, that both &func and plain func create a
pointer of the appropriate function pointer type.

However, for member function pointers it seems that the use of & is
mandatory, the following code compiles but does not when uncommenting
the initialization of c:

class A
{
public:
static void foo();
void bar();
};

void A::foo()
{}

void A::bar()
{}

int main()
{
typedef void (*func)();
func a(A::foo);
func b(&A::foo);

typedef void (A::*memfunc)();
//memfunc c(A::bar);
memfunc d(&A::bar);

return 0;
}

Is there a rationale behind this "inconsistency"? What would you
recommend as the correct way to retrieve a pointer of an static
function, with or without &?

Yours,
Daniel

--
Got two Dear-Daniel-Instant Messages
by MSN, associate ICQ with stress--so
please use good, old E-MAIL!
Aug 20 '07 #1
3 1588
On 2007-08-20 12:16, Daniel Kraft wrote:
Hi,

I was curious whether or not to use the & operator when creating a
function pointer. It seems, that both &func and plain func create a
pointer of the appropriate function pointer type.

However, for member function pointers it seems that the use of & is
mandatory, the following code compiles but does not when uncommenting
the initialization of c:

class A
{
public:
static void foo();
void bar();
};

void A::foo()
{}

void A::bar()
{}

int main()
{
typedef void (*func)();
func a(A::foo);
func b(&A::foo);

typedef void (A::*memfunc)();
//memfunc c(A::bar);
memfunc d(&A::bar);

return 0;
}

Is there a rationale behind this "inconsistency"? What would you
recommend as the correct way to retrieve a pointer of an static
function, with or without &?
I believe that this is due to some C-ism that has survived, since
there's nothing else you can mean when you write a statement like

func a = A::foo

they made the & optional. I however always uses & since it makes the
intent clear.

--
Erik Wikström
Aug 20 '07 #2
Erik Wikström wrote:
On 2007-08-20 12:16, Daniel Kraft wrote:
>Hi,

I was curious whether or not to use the & operator when creating a
function pointer. It seems, that both &func and plain func create a
pointer of the appropriate function pointer type.

However, for member function pointers it seems that the use of & is
mandatory, the following code compiles but does not when uncommenting
the initialization of c:

class A
{
public:
static void foo();
void bar();
};

void A::foo()
{}

void A::bar()
{}

int main()
{
typedef void (*func)();
func a(A::foo);
func b(&A::foo);

typedef void (A::*memfunc)();
//memfunc c(A::bar);
memfunc d(&A::bar);

return 0;
}

Is there a rationale behind this "inconsistency"? What would you
recommend as the correct way to retrieve a pointer of an static
function, with or without &?

I believe that this is due to some C-ism that has survived, since
there's nothing else you can mean when you write a statement like

func a = A::foo

they made the & optional. I however always uses & since it makes the
intent clear.
Yes, that is what I thought of and used to use &; however, later I
thought of that A::foo is some kind of "functor" as you can call it like
A::foo() as the function-pointer is and therefore "A::foo" *is* a
function pointer rather than something to obtain the address from...

Well, sounds weird, I know ;) However, this made me believe without & is
the more "correct" way... But as I found for member functions this
works only with &, I got suspicious... I think in the future I will
come back to & again.

Thank you!
Daniel
--
Got two Dear-Daniel-Instant Messages
by MSN, associate ICQ with stress--so
please use good, old E-MAIL!
Aug 20 '07 #3
On Aug 20, 3:16 pm, Daniel Kraft <d...@domob.euwrote:
Hi,

I was curious whether or not to use the & operator when creating a
function pointer. It seems, that both &func and plain func create a
pointer of the appropriate function pointer type.

However, for member function pointers it seems that the use of & is
mandatory, the following code compiles but does not when uncommenting
the initialization of c:

class A
{
public:
static void foo();
void bar();

};

void A::foo()
{}

void A::bar()
{}

int main()
{
typedef void (*func)();
func a(A::foo);
func b(&A::foo);

typedef void (A::*memfunc)();
//memfunc c(A::bar);
memfunc d(&A::bar);

return 0;

}

Is there a rationale behind this "inconsistency"? What would you
recommend as the correct way to retrieve a pointer of an static
function, with or without &?

Yours,
Daniel
The "function name" gets converted to the function pointer using
"implicit conversions. Thus, the following two give same result but
are not the same:

void foo();

typedef void (*fptr)();

fptr f1 = foo; //uses trivial conversion function name to function
pointer
fptr f2 = &foo; // no conversion, expression on the right-side is
already a pointer

In most of the cases, a function name gets implicitly converted into a
function pointer, and hence it doesnot matter what you write.
However, there are cases where it does matter:

int main()
{
sizeof(foo); //Error, sizeof cannot be applied to a function, and
sizeof doesnot allow function-name-to-pointer conversion on its
argument
sizeof(&foo); //fine, sizeof can be applied to a pointer
}

-N
Aug 20 '07 #4

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

Similar topics

5
by: Nimmi Srivastav | last post by:
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) { .... }
14
by: bo | last post by:
And why and where one should use one vs. the other? Verbally, it seems like semantics to me--but obviously there is some actual difference that makes references different and or preferable over...
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
12
by: arnuld | last post by:
this is exercise 2-3 from the mentioned section. i have created a solution for it but i see errors and i tried to correct them but still they are there and mostly are out of my head: ...
13
by: Chris Thomasson | last post by:
Here is some info on a C++ allocator prototype I am working on: http://groups.google.com/group/comp.lang.c++/browse_frm/thread/beeee1f61fdbb52c Any tried-and-true techniques for calculating the...
4
by: developereo | last post by:
Hi folks, Can anybody shed some light on this problem? class Interface { public: Interface() { ...} virtual ~Interface() { ...} virtual method() = 0; };
42
by: coder_lol | last post by:
Thanks everyone again for contributing to helping me clear C++ confusions. I did some serious reading on copy constructors and assignments and I think I've got a good handle on the memory stuff. ...
0
by: Ioannis Vranos | last post by:
Although not about C++ only, I think many people here have K&R2, so I post this message in clc++ too. In K&R2 errata page <http://www-db-out.research.bell-labs.com/cm/cs/cbook/2ediffs.html>...
0
by: David Thompson | last post by:
On Wed, 09 Apr 2008 12:34:43 +0500, arnuld <arnVuld@ippiVmail.com> wrote: I think you've got the idea, but: - I would be careful about using 'equal'. Pointers and arrays are different things,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.