LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-11-2010, 05:21 AM   #1
Kenny_Strawn
Senior Member
 
Registered: Feb 2010
Location: /usa/ca/orange_county/lake_forest
Distribution: ArchBang, Google Android 2.1 + Motoblur (on Motortola Flipside), Google Chrome OS (on Cr-48)
Posts: 1,791
Blog Entries: 62

Rep: Reputation: 56
Nested functions in C++


Is it possible to define functions within other functions in C++? I know it is the case in other languages.
 
Old 03-11-2010, 06:30 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by Kenny_Strawn View Post
Is it possible to define functions within other functions in C++? I know it is the case in other languages.
I'm not certain, but I think you need to wrap it in struct or class to get behavior similar to a nested function:

Code:
#include <cstdio>
int y=1;
int main(int argc, char**argv)
{
   int x=2;
   struct
   {
      void operator()(int c)
      {
         printf("%d\n", y+c);
      }
   } print;
   print(10);
   return 0;
}
The red part defines a nested functor named print. A functor serves the purpose of a function without exactly being a function.

But, depending on which other language you mean, you may be surprised and disappointed with the behavior. Local variables in the outer function are not available to the inner.

If you change the y in two places in that code to be x. You might expect it to print 12 instead of 11. Or you might expect it to print 11. Instead it gives a compile error because the x in main hides the global x without being available itself, so there is no x in scope in the nested functor.

Last edited by johnsfine; 03-11-2010 at 06:55 AM.
 
Old 03-12-2010, 12:21 AM   #3
Galib
Member
 
Registered: Mar 2009
Location: $HOME
Distribution: Slackware64
Posts: 69

Rep: Reputation: 17
I was looking around and ran into this. It says that gcc can't compile nested functions!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
OSE system call functions to Linux Sytem Call functions required roshantraj30 Linux - General 0 06-08-2009 02:06 AM
LXer: OpenOffice.org Calc functions, part 1: Understanding functions LXer Syndicated Linux News 0 03-31-2007 12:01 PM
Slackpkg: missing something in /usr/libexec/slackpkg/functions.d/dialog-functions.sh michelino Slackware 4 03-20-2007 12:22 PM
gSOAP problems with nested functions Mq2004 Programming 0 03-27-2005 04:26 AM
pointers to functions/member functions champ Programming 2 03-28-2003 06:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration