From 559924471070022192
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,fb1a92fa405c930c
X-Google-Attributes: gidf78e5,public
From: clamage@eng.sun.com (Steve Clamage)
Subject: Re: nested functions
Date: 1999/06/17
Message-ID: <7k9a31$42o$1@engnews1.eng.sun.com>#1/1
X-Deja-AN: 490730586
Approved: Valentin Bonnard <bonnard@clipper.ens.fr>
References: <7jd4c5$nup$1@nnrp1.deja.com> <7jjjr6$r02$1@nnrp1.deja.com> <7jma5c$i85@abyss.West.Sun.COM> <FD38vK.CC4@research.att.com> <7jrpss$ogk$1@nnrp1.deja.com> <FqA83.38464$wk2.553149@newscene.newscene.com> <37659D89.565ACF7D@technologist.com>
X-Original-Date: 16 Jun 1999 22:56:33 GMT
Organization: Sun Microsystems Inc., Mountain View, CA
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBVAwUAN2koH6wEuYhIxRhxAQHYxQH+OGAVizFAZRAGKHlIlm7bM25JJ5GE4HbK 8xiBsPpd/N46AWQ/0GSkBaCXsdXQtpOx6vMPkPTqea1CPoXuiKx6fQ== =vAAI
Newsgroups: comp.std.c++

David R Tribble <dtribble@technologist.com> writes:

>I disagree; I don't think it makes much sense to call or pass a
>pointer to a local function to functions outside its scope (i.e,
>outside its enclosing function).

On the contrary, it is very useful. It's common to see a pattern
like this in Pascal (but I'll use C++ syntax):

int main()
{
    void dosomething( bool(*fp)(int), int k )
    {
	...
	if( fp(k) ) // call the callback function
	    ...
	else
	    ...
    }

    void process1()
    {
	... decls

	bool getdata(int i) // the callback for process1
	{
	    ...
	}

	...

	dosomething(getdata);
    }
    ...
}

Function dosomething has a callback that uses its own context.
It can be called from outside the scope of the callback's
enclosing function.

The difference between C++ and Pascal is that Pascal does not
have free pointers to functions. A function can be passed to
another function, but saved in a variable. Consequently, you
cannot call a function with a function whose enclosing scope has
been exited. In C++, that programming error becomes possible.

--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]



