Article View: comp.lang.misc
Article #4281Re: c++ questions
From: Life is hard... and then you die...
Date: Thu, 12 Oct 1995 05:00
Date: Thu, 12 Oct 1995 05:00
60 lines
1990 bytes
1990 bytes
Edwin Tsang Wai-Kwong wrote:
>
> I know I cannot pass function as a parameter.
>
> 1. How can I pass a function as a parameter?
??? If you know you can't pass a function as a parameter, then
why are you asking? But you're right, you can't pass a function;
you can pass a pointer to a function however.
> e.g.
>
> void func1(){
>
> }
>
> void func2(void func1){
No, this declares func2 as taking one parameter of type void (which
is illegal). You want it to take a parameter of type pointer to
function:
void func2(void (*fun_ptr)()){
You can then call the function by simply adding ()'s to the
parameter name:
fun_ptr(); /* will call func1(), assuming the code below in main */
>
> }
>
> main()
> {
>
> func2(func1());
func1() is a function call, which returns nothing therefore making
this illegal too. w/o the ()'s func1 decays to a pointer to function
(much like for an array, where the name w/o the []'s causes it to
decay to a pointer to the element type). So therefore you want
func2(func1);
>
> }
>
Btw, if you have a c++ question, then why are you posting to
comp.lang.misc, comp.object, comp.lang.c and comp.lang.object?
Have you followed all these groups and found them to all be
relevant to your question? Anyway, your question is pure C so
it's not off-topic for c.l.c.
Cheers,
Ronald
Technical Details
Message-ID:
<307CA0E8.167EB0E7@psi.ch>Path:
arc1-ca.pugleaf.net!elna.ethz.ch!usenetReferences:
<45htdt$qdv@duke.cs.duke.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!elna.ethz.ch!usenet From: "Life is hard... and then you die." <Ronald.Tschalaer@psi.ch> Newsgroups: comp.lang.misc,comp.object,comp.lang.c++,comp.lang.c,comp.lang.object, Subject: Re: c++ questions Date: Thu, 12 Oct 1995 06:00:24 +0100 Organization: Swiss Federal Institute of Technology (ETHZ) Message-ID: <307CA0E8.167EB0E7@psi.ch> References: <45htdt$qdv@duke.cs.duke.edu> NNTP-Posting-Host: yggdrasil.ethz.ch Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Mozilla 2.0b1J (X11; I; SunOS 4.1.3 sun4m)