 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Timothy Madden Guest
|
Posted: Tue Dec 21, 2004 2:17 pm Post subject: 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
|
|
| Back to top |
|
 |
Tom Widmer Guest
|
Posted: Tue Dec 21, 2004 2:59 pm Post subject: Re: References to functions ? |
|
|
Timothy Madden wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
void Guest
|
Posted: Tue Dec 21, 2004 3:10 pm Post subject: Re: References to functions ? |
|
|
Timothy Madden wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Tue Dec 21, 2004 3:18 pm Post subject: Re: References to functions ? |
|
|
Tom Widmer wrote:
| Quote: | [...]
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
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Tue Dec 21, 2004 3:22 pm Post subject: Re: References to functions ? |
|
|
void wrote:
| Quote: | 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
void Foo(fun_object f, void *data)
???
| Quote: | {
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
|
|
| Back to top |
|
 |
Timothy Madden Guest
|
Posted: Tue Dec 21, 2004 5:11 pm Post subject: Re: References to functions ? |
|
|
"Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> wrote
| Quote: | 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
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
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
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Tue Dec 21, 2004 5:16 pm Post subject: Re: References to functions ? |
|
|
Timothy Madden wrote:
| Quote: | "Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> wrote in message
news:IcXxd.12517$Ae.8647 (AT) newsread1 (DOT) 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
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
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 :-)
| Quote: | 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
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|