Board index » Visual Studio » nested functions in VC++
|
YBD
|
|
YBD
|
nested functions in VC++
Visual Studio65
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 - |
| Carl
Registered User |
Fri Apr 22 00:43:53 CDT 2005
Re:nested functions in VC++
alexl wrote:
QuoteI think what I am looking for is a "nested function" which C++ doesn't 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 - |
| alexl
Registered User |
Fri Apr 22 01:21:56 CDT 2005
Re:nested functions in VC++
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 */ ); - |
| Tom
Registered User |
Fri Apr 22 03:54:37 CDT 2005
Re:nested functions in VC++
alexl wrote:
QuoteThanks! Trying to accomplish the following. Should print "it worked." 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 - |
| Murrgon
Registered User |
Fri Apr 22 08:58:39 CDT 2005
Re:nested functions in VC++
alexl wrote:
QuoteI think what I am looking for is a "nested function" which C++ doesn't 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 - |
| alexl
Registered User |
Fri Apr 22 17:23:01 CDT 2005
Re:nested functions in VC++
thanks!
- |
| Simon
Registered User |
Sun Apr 24 18:06:21 CDT 2005
Re:nested functions in VC++
"Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@mvps.org.nospam>
wrote in message> QuoteWhy do you think you want nested functions? 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. - |
