local class question

V

Victor Liu

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.
 
C

Claudio Puviani

Victor Liu said:
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
 
T

Thomas Matthews

Victor said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,128
Latest member
ElwoodPhil
Top