Which scope is searcheg first ?

R

Razvan

Hi,




I took a C++ test and I found the following question:



Q. Inside a class member function definition, which scope is searched
first when an unqualified variable is accessed?

1.Class static data
2.Global namespace data
3.Function local data
4.Current namespace data
5.Class member data




If I understand the question correctly I have to tell where
the compiler is looking for a variable when I use its symbol. To my
understanding the answer is "Function local data". That means that
inside a member function when I am using the variable myVar it will
first try to see if I have a local variable named myVar. If such a
variable does not exist then it will see if I have a member variable
myVar. If such one is not defined then it will look in the current
namespace and then in the global namespace.

So the 'scope' order is:
a. function local data
b. class member data (the same as static member data); I mean they are
both members of the class and the fact that the data is static or not
is irrelevant from this point of view (scope order evaluation)
c. current name space
d. global name space


Please correct me if I am wrong.



Regards,
 
V

Victor Bazarov

Razvan said:
I took a C++ test and I found the following question:



Q. Inside a class member function definition, which scope is searched
first when an unqualified variable is accessed?

1.Class static data
2.Global namespace data
3.Function local data
4.Current namespace data
5.Class member data




If I understand the question correctly I have to tell where
the compiler is looking for a variable when I use its symbol. To my
understanding the answer is "Function local data". That means that
inside a member function when I am using the variable myVar it will
first try to see if I have a local variable named myVar. If such a
variable does not exist then it will see if I have a member variable
myVar. If such one is not defined then it will look in the current
namespace and then in the global namespace.

So the 'scope' order is:
a. function local data
b. class member data (the same as static member data); I mean they are
both members of the class and the fact that the data is static or not
is irrelevant from this point of view (scope order evaluation)
c. current name space
d. global name space


Please correct me if I am wrong.

Seems fine. Basically, the rule is "from the most enclosed scope
to the global scope".

Victor
 
J

Jerry Coffin

(e-mail address removed) (Razvan) wrote in message
[ ... ]
I took a C++ test and I found the following question:



Q. Inside a class member function definition, which scope is searched
first when an unqualified variable is accessed?

1.Class static data
2.Global namespace data
3.Function local data
4.Current namespace data
5.Class member data

Technically, none of the above. _Block_ local data is searched first.
Just for example:

int func() {
int a; // function local data

if ( something) {
float a; // block local data

a = 1; // unqualified variable access.

In this case, the function-local data is NOT used -- the block-local
data is what is accessed. For the most part, the rule is pretty
simple though: searching starts at the most local scope and progresses
outward to the least local scope.

There are exceptions to this though -- for an obvious one, a using
declaration can "teleport" the names from some namespace into the
local scope. Templates also have some oddities because searching
happens both in the scope where the template is instantiated AND the
scope where it is defined (I'm simplifying a bit, but hopefully I'm
portraying the basic idea). In the usual case, that's pretty
straightforward, but when/if you export a template, it can get
substantially more complex.
 
R

Razvan

Hello,




So, the correct order is:

a. block local data
b. function local data
c. class member data (the same as static member data); I mean they are
both members of the class and the fact that the data is static or not
is irrelevant from this point of view (scope order evaluation)
d. current name space
e. global name space

There are exceptions to this though -- for an obvious one, a using
declaration can "teleport" the names from some namespace into the
local scope. Templates also have some oddities because searching

For example:

void func ()
{

{
using namespace SomeNamespaceABC;
func_from_namespace_ABC();
}

func_from_namespace_ABC(); // here it fails !? the namespace is
not in scope any more ?!
}

That means the 'using' clause is valid for the current scope.
Right ?


happens both in the scope where the template is instantiated AND the
scope where it is defined (I'm simplifying a bit, but hopefully I'm

What are you trying to say about templates ? Can you give me
an example ? Or perhaps just point me to some documents on the matter.


Thanks,
Razvan
 
F

Fraser Ross

"Razvan"
void func ()
{

{
using namespace SomeNamespaceABC;
func_from_namespace_ABC();
}

func_from_namespace_ABC(); // here it fails !? the namespace is
not in scope any more ?!
}

That means the 'using' clause is valid for the current scope.
Right ?
Yeah, but Jerry spoke about a using-declaration and you give an example with
a using-directive and you have made your own term for it.

Fraser.
 
R

Razvan

Yeah, but Jerry spoke about a using-declaration and you give an example with
a using-directive and you have made your own term for it.

What is the difference between a 'using declaration' and a
'using directive' ?



Razvan
 
F

Fraser Ross

A using-directive brings into scope the contents of a namespace. A
using-declaration only brings into scope a name. The name could be that of
more than one entity e.g. a function and an POD type. A using-declaration
brings into scope only previously declared names. A using-directive brings
into scope everything declared before and after it.

Fraser.




"Razvan"
 

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
474,171
Messages
2,570,935
Members
47,472
Latest member
KarissaBor

Latest Threads

Top