473,387 Members | 1,791 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,387 software developers and data experts.

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 1508
"Victor Liu" <vi***********@cyberpowersystemsnospam.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::goodbye()
// { 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::goodbye()
{ 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()::Anything::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::goodbye 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.learn.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
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...
3
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
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...
9
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
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...
55
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...
1
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...
2
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...
7
by: pauldepstein | last post by:
#include <iostream> using namespace std; double & GetWeeklyHours() { double h = 46.50; double &hours = h; return hours; }...
11
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. ...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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,...

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.