473,406 Members | 2,208 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,406 software developers and data experts.

References to functions ?

Hello

I have recently read in a fairy good book ('C++ Templates: The Complete
Guide', by
David Vandevoorde, Nicolai M. Josuttis) that "both references to objects and
references to functions are acceptable" as template parameters.

My question is: Is there such thing as a reference to a function ? What
exactly does it mean and how could I use such a reference? Has someone used
this before ?

Thank you
"Timothy Madden"
Romania
Jul 22 '05 #1
6 1268
Timothy Madden wrote:
Hello

I have recently read in a fairy good book ('C++ Templates: The Complete
Guide', by
David Vandevoorde, Nicolai M. Josuttis) that "both references to objects and
references to functions are acceptable" as template parameters.

My question is: Is there such thing as a reference to a function ? What
exactly does it mean and how could I use such a reference? Has someone used
this before ?


A reference to a function is no different to any other reference. e.g.

int f(int);

int (*fptr)(int) = f; //or &f
int (&fref)(int) = f; //fref isn't assignable, since functions aren't.

int i = f(10);
int j = fptr(10); //or int j = (*fptr)(10)
int k = fref(10);

So you use a function reference where ever you want a reference to a
function, as opposed to a pointer to a function, or the function itself.

Tom
Jul 22 '05 #2
Timothy Madden wrote:
Hello

I have recently read in a fairy good book ('C++ Templates: The Complete
Guide', by
David Vandevoorde, Nicolai M. Josuttis) that "both references to objects and
references to functions are acceptable" as template parameters.

My question is: Is there such thing as a reference to a function ? What
exactly does it mean and how could I use such a reference? Has someone used
this before ?


Consider this example:

template<typename fun_object>
void Foo(fun_object &f, void *data)
{
f(data);
}

void some_function(void *data)
{
// does something with data
}

class FunObj
{
public:
void operator()(void *data)
{
// does something with data
}
};

int main()
{
void *data;
FunObj f;
Foo(some_function, data);
Foo(f, data);
return 0;
}

Best
Darek
Jul 22 '05 #3
Tom Widmer wrote:
[...]
So you use a function reference where ever you want a reference to a
function, as opposed to a pointer to a function, or the function itself.


"As opposed"? You mean that you wouldn't use a function pointer where
ever you want a pointer to a function (as opposed to a reference)?

And could you please explain the first part of that sentence? "Use
a function reference where ever you want a reference to a function"?
What does that mean? Is the meaning the same as in "use an int pointer
wherever you want a pointer to int"? Would you say that it's the same
as "use a bar stool wherever you need a stool in a bar"?

I am not a native English speaker, you see, that's why I am asking.

Thanks!

V
Jul 22 '05 #4
void wrote:
Timothy Madden wrote:
Hello

I have recently read in a fairy good book ('C++ Templates: The Complete
Guide', by
David Vandevoorde, Nicolai M. Josuttis) that "both references to
objects and
references to functions are acceptable" as template parameters.

My question is: Is there such thing as a reference to a function ? What
exactly does it mean and how could I use such a reference? Has someone
used
this before ?

Consider this example:

template<typename fun_object>
void Foo(fun_object &f, void *data)


Here is my question: except due to a mistake, why would one want to have
the '&' here in the first argument? Why not just write

template<typename fun_object>
void Foo(fun_object f, void *data)

???
{
f(data);
}

void some_function(void *data)
{
// does something with data
}

class FunObj
{
public:
void operator()(void *data)
{
// does something with data
}
};

int main()
{
void *data;
FunObj f;
Foo(some_function, data);
Foo(f, data);
return 0;
}


Yes, in your example, a reference to a function is formed (likely due to
some mistake in the argument declaration). And, yes, it's _legal_. The
question remains, however, why would one _need_ to use a reference to
a function?

Thanks.

V
Jul 22 '05 #5

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:Ic*****************@newsread1.dllstx09.us.to. verio.net...
void wrote:
Timothy Madden wrote:
Hello

I have recently read in a fairy good book ('C++ Templates: The Complete
Guide', by
David Vandevoorde, Nicolai M. Josuttis) that "both references to
objects and
references to functions are acceptable" as template parameters.

My question is: Is there such thing as a reference to a function ? What
exactly does it mean and how could I use such a reference? Has someone
used
this before ?


Consider this example:

template<typename fun_object>
void Foo(fun_object &f, void *data)


Here is my question: except due to a mistake, why would one want to have
the '&' here in the first argument? Why not just write

template<typename fun_object>
void Foo(fun_object f, void *data)

???

Please do not be so mad. The poster really answered my question and helped
me.

I could want to have the '&' in the first argument if my class in the actual
parameter is not copy-contructable or if it represents or consumes some
external resource and is designed with RAII so the constructor allocates
resources or for the case my fun_object class is a single-ton class. And
there is allways, of course, the case when my fun_object class is huge and I
do not need it copied for the purpose of function Foo

"Timothy Madden"
Romania
Jul 22 '05 #6
Timothy Madden wrote:
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:Ic*****************@newsread1.dllstx09.us.to. verio.net...
void wrote:
Timothy Madden wrote:
Hello

I have recently read in a fairy good book ('C++ Templates: The Complete
Guide', by
David Vandevoorde, Nicolai M. Josuttis) that "both references to
objects and
references to functions are acceptable" as template parameters.

My question is: Is there such thing as a reference to a function ? What
exactly does it mean and how could I use such a reference? Has someone
used
this before ?
Consider this example:

template<typename fun_object>
void Foo(fun_object &f, void *data)
Here is my question: except due to a mistake, why would one want to have
the '&' here in the first argument? Why not just write

template<typename fun_object>
void Foo(fun_object f, void *data)

???


Please do not be so mad.


Mad? Do I really come across as mad? I am sorry. It was by no means my
intention. I am trying to learn C++ just like all of us here. That's why
I asked. Perhaps I _am_ mad if my hopes are so high :-)
The poster really answered my question and helped
me.

I could want to have the '&' in the first argument if my class in the actual
parameter is not copy-contructable or if it represents or consumes some
external resource and is designed with RAII so the constructor allocates
resources or for the case my fun_object class is a single-ton class. And
there is allways, of course, the case when my fun_object class is huge and I
do not need it copied for the purpose of function Foo


Hey, that's a very good explanation. Thank you. I've not considered the
use of functors that are not copy-constructible.

V
Jul 22 '05 #7

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

Similar topics

9
by: peter | last post by:
Hello all, Recently I've started to refactor my code ...(I'm using python 2.3.4) I tried to add extra functionality to old functions non-intrusively. When I used a construct, which involves...
17
by: Tom | last post by:
The motivation for references seems clear: stop people from using nasty pointers when all they really want is a reference to an object. But C++ references are so inadequate that I'm still using...
5
by: John | last post by:
Hi I have my app in access 2000 and the office version I have is 10.0 (xp). When I send my app to clients (who have office 9.0/2000) as mde, how does my application work around the reference...
3
by: SAM | last post by:
Hi everyone, I consider myself a very competent programmer when it comes to actual programming and analyzing the business that I'm modelling. I am now crossing into what I would consider Access...
14
by: el_sid | last post by:
Our developers have experienced a problem with updating Web References in Visual Studio.NET 2003. Normally, when a web service class (.asmx) is created, updating the Web Reference will...
28
by: Frederick Gotham | last post by:
When I was a beginner in C++, I struggled with the idea of references. Having learned how to use pointers first, I was hesitant to accept that references just "do their job" and that's it. Just...
9
by: igor.kulkin | last post by:
References is a relatively basic feature of C++ language. It might be a good thing to think of references as aliases to the variables. However it's good to think of references this way when you...
9
by: Rahul | last post by:
Hi Everyone, I was wondering about references to functions, so i tried this, int (& p) (); // doesn't work as array of references is illegal as memory is not allocated for references,...
0
by: Irfy | last post by:
Could someone elaborate on the purpose and concrete usage scenarios of references to functions. What can references to functions do that pointers to functions cannot. Why are they in C++? A swap...
3
by: mk | last post by:
Hello everyone, I'm storing functions in a dictionary (this is basically for cooking up my own fancy schmancy callback scheme, mainly for learning purpose): .... return "f2 " + arg .......
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
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.