473,853 Members | 1,984 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 12207

"A" <A@iprimus.com. au> wrote in message
news:3f******** @news.iprimus.c om.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**********@y ahoo.com> wrote in message
news:dRabb.558$ La.517@fed1read 02...

"A" <A@iprimus.com. au> wrote in message
news:3f******** @news.iprimus.c om.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.p l> 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 "functionat e".
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
2350
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
2574
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: (http://www.python.org/search/hypermail/python-1993/0343.html) "This is because nested function definitions don't have access to the local variables of the surrounding block -- only to the globals of the
2
7519
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 in the nested query by specifying dates. These would somehow need to be passed to the nested query. I've looked into stored procedures/functions but am still stumped on
7
2258
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
3254
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 complexity analyzer tool supposedly does not pick it up. Is it really more efficient? Personally I find this coding style extremely cryptic, misleading and error-prone. I believe that I have removed all traces of proprietary-ness from this coding...
9
2861
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
2318
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 could implement an equivalent behaviour with plain C (in this case I'm thinking of the language I'm developing, which shall be converted into C for target compilation). So far I didn't touch the topic "nested functions", since I just don't see an...
2
1672
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 and its nested children all in one module, declare the children static - voila. Std-C. Regards, Johannes
9
3994
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 (most recent call last):
0
9748
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10670
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10735
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10358
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9506
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5736
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4549
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.