473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Nested functions in C++

A
Hi,

How do you make use of nested functions in C++? I realize in C++ that
everything must be declared first in a header file before implementation in
a .cpp file. I tried to nest a method prototype in another prototype but
seems pointless. Can someone please write a short, simple, and concise
skeleton code of how to use nested functions?

class Foo
{
private:
int a;
int b;

public:
void funcA();
void funcB(); // seems pointless
}

void Foo:: funcA()
{
this->funcB;
void Foo::funcB()
{...}
}
//error: local method problem

any help appreciated.

Regards
reuytrt
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.518 / Virus Database: 316 - Release Date: 11/09/2003
Jul 19 '05 #1
6 12180

"A" <A@iprimus.com.au> wrote in message
news:3f********@news.iprimus.com.au...
Hi,

How do you make use of nested functions in C++? I realize in C++ that
everything must be declared first in a header file before implementation in a .cpp file. I tried to nest a method prototype in another prototype but
seems pointless. Can someone please write a short, simple, and concise
skeleton code of how to use nested functions?

class Foo
{
private:
int a;
int b;

public:
void funcA();
void funcB(); // seems pointless
}

void Foo:: funcA()
{
this->funcB;
void Foo::funcB()
{...}
}
//error: local method problem

any help appreciated.

Regards
reuytrt
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.518 / Virus Database: 316 - Release Date: 11/09/2003


Functions may not be nested in C++. Nor is it *required* that declarations
appear in a header file, but good design generally dictates that interface
and implementation be separated.

Hope this helps!
Jul 19 '05 #2
A wrote:
Hi,

How do you make use of nested functions in C++?


you can't...

-- Nuclear / the Lab --

Jul 19 '05 #3
On Sun, 21 Sep 2003 15:01:30 +0930, "A" <A@iprimus.com.au> wrote:
How do you make use of nested functions in C++?
Before you can use them you must have them.

C++ does not support nested function à la Pascal, but does support a
limited form of local classes (nested in functions).

You can use local classes to achieve logical nesting, but a member
function of a local class doesn't have access to the arguments and
local variables of the enclosing function unless you provide such
access yourself, e.g. via reference constructor arguments.
I realize in C++ that everything must be declared first in a header
file before implementation in a .cpp file.
That is incorrect; the C++ standard does not even mention files.

I tried to nest a method prototype in another prototype but
seems pointless. Can someone please write a short, simple, and concise
skeleton code of how to use nested functions?

class Foo
{
private:
int a;
int b;

public:
void funcA();
void funcB(); // seems pointless
}
It is, indentation is not equal to logical nesting.

void Foo:: funcA()
{
this->funcB;
void Foo::funcB()
{...}
}
//error: local method problem

any help appreciated.


The best you can do is forget it, then investigate the issue anew
in a few years time.

Jul 19 '05 #4

"Dave Theese" <ch**********@yahoo.com> wrote in message
news:dRabb.558$La.517@fed1read02...

"A" <A@iprimus.com.au> wrote in message
news:3f********@news.iprimus.com.au...
Hi,

How do you make use of nested functions in C++? I realize in C++ that
everything must be declared first in a header file before implementation in
a .cpp file. I tried to nest a method prototype in another prototype but
seems pointless. Can someone please write a short, simple, and concise
skeleton code of how to use nested functions?

class Foo
{
private:
int a;
int b;

public:
void funcA();
void funcB(); // seems pointless
}

void Foo:: funcA()
{
this->funcB;
void Foo::funcB()
{...}
}
//error: local method problem

any help appreciated.

Regards
reuytrt


Functions may not be nested in C++. Nor is it *required* that

declarations appear in a header file, but good design generally dictates that interface
and implementation be separated.

Hope this helps!

Surely there must be a solution to this problem. The reason i wanted to do
this is because i want to be able to group related functions together. For
example a function that calls another related function to solve a problem
(this can be seen in indirect recursion calls).
Regards
ewrewrwer
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.510 / Virus Database: 307 - Release Date: 14/08/2003
Jul 19 '05 #5
On Sun, 21 Sep 2003 18:53:06 +0930, Ying Yang wrote:
Surely there must be a solution to this problem. The reason i wanted to do
this is because i want to be able to group related functions together. For


This is exactly why C++ was invented - to group functions together into
classes. Look at following (incorrect) code:

int f(int i)
{
int j = i + 1;
int a() // local function is invalid!
{
return j * i;
};

return a() + i;
}

it won't work, but you can use class instead:

class f {
const int& i; // variables shared between all ...
int j; // ... functions grouped into class
int ret; // return value
int a() // "internal" function
{
return j * i;
}
public:
f(const int& arg_i) : i(arg_i), j(i + 1)
{
ret = a() + i;
}

operator int() {return ret;}
// bonus: alternate return type from function object
operator std::string() {return "hi!";}
};

You may call your function object simply creating it:
int r = f(3);

you may also retrieve its "alternate" return value:
std::string q = f(4);

regards
B.
Jul 19 '05 #6
Bronek Kozicki <br**@rubikon.pl> wrote:
# Look at following (incorrect) code:

# int f(int i)
# {
# int j = i + 1;
# int a() // local function is invalid!
# {
# return j * i;
# };
#
# return a() + i;
# }

# it won't work, but you can use class instead:

Maybe, but this will not replace the local (or nested, as you like) functions.
It just tries to help with the problem they deal with.

Local functions have an access to the local environment of the parent function.
You cannot achieve this in C++. To have local functions you would like also
to have a mechanism of closures, as it is done for example in boost::phoenix.

# You may call your function object simply creating it:
# int r = f(3);

# you may also retrieve its "alternate" return value:
# std::string q = f(4);

Fine, but you still have to pass arguments to such a "functionate".
You have declared a local function in this (incorrect) code before,
which does not need to be passed an argument; it takes the value from
the local environment from the parent function. You cannot simulate
this anyhow in C++ (unless you directly pass a "closure" as an argument :).

Regards,
--
1 6 1 7 4 4 2 548 g4bc7a4 66z 3xt7w v1y z9p1 120 32
(( Michal "Sektor" Malecki w4 66 64 73 7564 24 5 v 34 4
)) ektor van Skijlen 1 5 5 1 844 a v r z 4
Software engineer, Motorola GSG Poland 1 2 2a 1 4
WARNING: Opinions presented by me on usenet groups are my personal opinions
ONLY and are not connected to the employer.
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Nils Grimsmo | last post by:
hi, i'm having some trouble nesting functions. consider the following: def h(): x = 1 def g(): print x # ok, x is taken from h g()
6
by: Andy Baker | last post by:
Hi there, I'm learning Python at the moment and trying to grok the thinking behind it's scoping and nesting rules. I was googling for nested functions and found this Guido quote:...
2
by: Forgone Conclusion | last post by:
Hi, I have a View that groups and sums up totals. This View is then nested within in another View and used (it needs to be done like this). What i need to do is to be able to vary the records...
7
by: block111 | last post by:
Hello, code like this: int f1(int x){ int f2(int y){ return y*y; } if(x > 0) return f2(x);
10
by: nimmi_srivastav | last post by:
Below you will see an example of a nested conditional expression that this colleague of mine loves. He claims that it is more efficient that a multi-level if-else-if structure. Moreover, our...
9
by: Gregory Petrosyan | last post by:
I often make helper functions nested, like this: def f(): def helper(): ... ... is it a good practice or not? What about performance of such constructs?
4
by: Wolfgang Draxinger | last post by:
If you know languages like Python or D you know, that nested functions can be really handy. Though some compilers (looking at GCC) provide the extension of nested functions, I wonder, how one...
2
by: Johannes Bauer | last post by:
Nick Keighley schrieb: Why is there actually a *need* for nested functions? If functionality of subfunctions which are only locally visible is desired, why not put the nesting function parent...
9
by: Gabriel Rossetti | last post by:
Hello, I can't get getattr() to return nested functions, I tried this : .... def titi(): .... pass .... f = getattr(toto, "titi") .... print str(f) .... Traceback...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.