I think what I am looking for is a "nested function" which C++ doesn't
officially
support. Is the following workaround possible?


1. create a pointer to a function of type void fun ()
2. allocate memory for it
3. populate memory so newly created function does what I want
4. return pointer to said function

thx, -Alex

Re: nested functions in VC++ by Carl

Carl
Fri Apr 22 00:43:53 CDT 2005

alexl wrote:
> I think what I am looking for is a "nested function" which C++ doesn't
> officially
> support. Is the following workaround possible?
>
>
> 1. create a pointer to a function of type void fun ()
> 2. allocate memory for it
> 3. populate memory so newly created function does what I want
> 4. return pointer to said function

Why do you think you want nested functions?

Typically, the only things that nested functions add are:

1. more limited scope of name visibility.
2. access to "up level" local variables (if c() is in b() which is in a(),
c() can access b()'s local variables as well as a()'s).

Use of classes instead of free functions more or less supplies 1. 2 can be
accomplished manually by passing references to the required up-level
variables.

What is it that you're trying to achieve?

-cd



Re: nested functions in VC++ by alexl

alexl
Fri Apr 22 01:21:56 CDT 2005

Thanks! Trying to accomplish the following. Should print "it worked."

typedef void(*ptf)();

class Hello {

void caller(){
ptf =cplusplusbind(this, m_callback);
..
call_fun_indirectly(ptf);
}

void m_callback () {
cout << "it worked!\n";
}

};

int main(int argc, char* argv[])
{
Hello h;
h.caller();
return 0;

}

ptf cplusplusbind (class T, T::*) {

return (/* do magic */ );


Re: nested functions in VC++ by Tom

Tom
Fri Apr 22 03:54:37 CDT 2005

alexl wrote:
> Thanks! Trying to accomplish the following. Should print "it worked."
>
> typedef void(*ptf)();
>
> class Hello {
>
> void caller(){
> ptf =cplusplusbind(this, m_callback);
> ..
> call_fun_indirectly(ptf);
> }
>
> void m_callback () {
> cout << "it worked!\n";
> }
>
> };
>
> int main(int argc, char* argv[])
> {
> Hello h;
> h.caller();
> return 0;
>
> }
>
> ptf cplusplusbind (class T, T::*) {
>
> return (/* do magic */ );
>

Boost helps here:

boost::function<void()> ptf;

class Hello {
void caller(){
ptf = boost::bind(&Hello::m_callback, this);
..
ptf();
}

void m_callback () {
cout << "it worked!\n";
}
};

int main(int argc, char* argv[])
{
Hello h;
h.caller();
return 0;
}

See boost.function and boost.bind (both being added to library extension
that is being added to standard C++), at www.boost.org.

Tom

Re: nested functions in VC++ by Murrgon

Murrgon
Fri Apr 22 08:58:39 CDT 2005

alexl wrote:
> I think what I am looking for is a "nested function" which C++ doesn't
> officially support.

Actually, you can put a function within a function, but not in the
normal sense. Try something like this:

void MyClass::SomeFunction()
{
struct Inner
{
static int InnerWorkings(int i)
{
return i * 4;
}
};

printf("Inner function call result: %d\n", Inner::InnerWorkings(5));
}

You would have to wonder why one would do this because for the
most part you shouldn't need to. However on a few rare occasions
it does come in handy.

Murrgon

Re: nested functions in VC++ by alexl

alexl
Fri Apr 22 17:23:01 CDT 2005

thanks!


Re: nested functions in VC++ by Simon

Simon
Sun Apr 24 18:06:21 CDT 2005

"Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@mvps.org.nospam>
wrote in message >
> Why do you think you want nested functions?
> Typically, the only things that nested functions add are:
>
> 1. more limited scope of name visibility.
> 2. access to "up level" local variables (if c() is in b() which is in a(),
> c() can access b()'s local variables as well as a()'s).
>
> Use of classes instead of free functions more or less supplies 1. 2 can
> be accomplished manually by passing references to the required up-level
> variables.

Indeed. The common case where I want nested functions is in recursive
functions, to supply the outer case. Typically, a recursive function may act
on some state, and either we pass it down the stack every time or create an
outer function which it can access. Generally now I use an anonymous
namespace to deal with this, but this still is polluting the namespace, be
it only by one name.

I *think* I have a reasonable argument that nested functions are just
anonymous classes (in the Java sense) that have names, but I'm not entirely
sure.

S.

"Whereof one cannot speak, thereof one must be silent." -- Wittgenstein.
"If you don't know, shut up." -- The wife.