473,885 Members | 2,538 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

local class question

hi,

why n1 in local::f() is no allowed ?

int n0;
void function() {

int n1;
static int n2;
class local {
int n3;
void f() {
n0;
n1;
n2;
n3;
}
};
}

Victor Liu.
Jul 22 '05 #1
2 1531
"Victor Liu" <vi***********@ cyberpowersyste msnospam.com.tw > wrote
hi,

why n1 in local::f() is no allowed ?

int n0;
void function() {

int n1;
static int n2;
class local {
int n3;
void f() {
n0;
n1;
n2;
n3;
}
};
}


Because n1 is an automatic variable which exists ONLY during an invocation of
'function'. Class 'local' exists before 'function' is ever invoked. Therefore,
class 'local' couldn't possibly see 'n1'. Think of it another way: where would
the code look for 'n1' (which SOMETIMES resides on the stack)?

Claudio Puviani
Jul 22 '05 #2
Victor Liu wrote:
hi,

why n1 in local::f() is no allowed ?

int n0;
void function() {

int n1;
static int n2;
class local {
int n3;
void f() {
n0;
n1;
n2;
n3;
}
};
}

Victor Liu.


Well, I thought this interesting so I did some
research:
#include <iostream>
using namespace std;

void
Function(void)
{
struct Anything
{
int var;
/* [1] */ void hello()
{ cout << "*** hello ***\n";}
void goodbye();
} my_var;
// void Anything::goodb ye()
// { cout << "goodbye\n" ;}

cout << "Entering Function()\n";
my_var.hello();
my_var.var = 5;
cout << "my_var.var == " << my_var.var << endl;
cout << "Leaving Function()\n";
return;
}

/* [2]Line 25: */ void Anything::goodb ye()
{ cout << "goodbye\n" ;}

int main(void)
{
cout << "main() -- Start\n";
Function();
cout << "main() -- End\n";
return 0;
}
In the above program, I get the errors (from g++):
TH009MA@th009ma-shl2-01 /cygdrive/d/temp
$ g++ -o junk junk.cpp
junk.cpp:25: error: syntax error before `::' token

Which is expected due to the scoping rules. The
structure inside Function() is not accessible outside
of Function().
When the definition at lines 25 & 26 are commented-out
and the definition inside the function are uncommented,
I get the error:
TH009MA@th009ma-shl2-01 /cygdrive/d/temp
$ g++ -o junk junk.cpp
junk.cpp: In function `void Function()':
junk.cpp:15: error: declaration of `void Function()::Any thing::goodbye( )'
outside of class is not definition
junk.cpp:15: error: syntax error before `{' token
junk.cpp: At global scope:
junk.cpp:17: error: syntax error before `<<' token
junk.cpp:18: error: syntax error before `.' token
junk.cpp:19: error: syntax error before `.' token
junk.cpp:20: error: syntax error before `<<' token
junk.cpp:21: error: syntax error before `<<' token
If I comment out the Anything::goodb ye method,
leaving the Anything::hello method, I get:
TH009MA@th009ma-shl2-01 /cygdrive/d/temp
$ g++ -o junk junk.cpp

TH009MA@th009ma-shl2-01 /cygdrive/d/temp
$ ./junk
main() -- Start
Entering Function()
*** hello ***
my_var.var == 5
Leaving Function()
main() -- End
So my conclusion is thus:
1. Nested functions are not allowed in C++.
2. External definitions of structures defined in a function
are not allowed because of note 1.
3. Inline function definitions are allowed in a structure
because their definition is not nested within the function.

I believe someone will reply with a quote from the standard.
{My copy is not available right now.}

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #3

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

Similar topics

3
2892
by: Tim Hill | last post by:
I'm just getting up to speed on Java, and have a question about local class behavior. Here's a mini-app to illustrate: interface Foo { void print(); } class LocalClassTest { public static void main(String args) { Foo foo = getFooImpl(100); foo.print();
3
3335
by: Nan Li | last post by:
I just discovered you can have a local class defined inside a function like this: #include <iostream> using namespace std; int main(int argc, char* argv) {
0
2502
by: Manfred Braun | last post by:
Hi All, I've already asked another question , but this is finally the same issue. I cannot execute two asynchron WMI queries on the local machine, one after the other. The original question is based on complicated code. So I reduced the "kernel" to a small C# sample , which shows the problem. The question is: - Is this a problem with my code, as not freeing resources etc? - Is this possibly a WMI issue?
9
2215
by: Stefan Turalski \(stic\) | last post by:
Hi, I done sth like this: for(int i=0; i<10; i++) {...} and after this local declaration of i variable I try to inicialize int i=0;
23
4061
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people who will find them very helpfull. gcc has them as a non-standard option, but only when compiling C language code, so I'm afraid there might be some obscure reason why local functions are not so easy to be dealt with in C++, which I do not yet know.
55
6285
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in C# in some way? Or maybe no, because it is similar to a global variable (with its scope restricted) which C# is dead against? Zytan
1
3648
by: jcprince | last post by:
Hi Not sure I can do what I'm trying to do without using a 3rd party component like Dart. I need to build a windows service to create a socket connection on an IBM mainframe using an IP and port combination. No problem there. However, due to the expected volume (at least 20x10K streams per second in each direction), the mainframe sysadmin has requested the service use multiple 'conversations' within the single IP connection. The...
2
1999
by: A.J. Bonnema | last post by:
Hi all, I just started using Python. I used to do some Java programming, so I am not completely blank. I have a small question about how classes get instantiated within other classes. I have added the source of a test program to the bottom of this mail, that contains 3 methods within a testclass that each instantiate the same class and bind it to a local variable. My understanding was, that the local variable gets garbage collected as...
7
6696
by: pauldepstein | last post by:
#include <iostream> using namespace std; double & GetWeeklyHours() { double h = 46.50; double &hours = h; return hours; } //---------------------------------------------------------------------------
11
3013
by: cj | last post by:
Perhaps someone knows how to do this. When I open a new ASP.NET web service application in VS 2008, it opens with a simple Hello World web service already written. I want to see this work. Without changing a thing I built the solution and published it to localhost default web site. How can I test it?????
0
9799
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,...
1
10872
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
10429
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
9592
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
7139
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
5808
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
4627
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
2
4235
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3245
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.