Re: Which scope is searcheg first ?

From: Jerry Coffin (jcoffin_at_taeus.com)
Date: 06/16/04


Date: 15 Jun 2004 19:56:19 -0700

mihai11@mailcity.com (Razvan) wrote in message news:<15f19d61.0406151238.1bdf0f17@posting.google.com>...

[ ... ]

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

-- 
    Later,
    Jerry.
The universe is a figment of its own imagination.


Relevant Pages

  • Re: Need help with Python scoping rules
    ... When a class definition is entered, a new namespace is created, ... "fact" in the class scope, and the assignment statement looks up ... Its just, during class creation, the local scope. ... When you enter a class definition, a new namespace is created, and that is used as the local scope during the class's definition. ...
    (comp.lang.python)
  • Re: Scope and program structure problems
    ... then I'd be declaring the variables/objects explicitly ... I personnally find the assignment, import, class and def statements (all having a binding behaviour) to be rather explicit. ... their scope according to how/where they were declared. ... Names bound within a class statement lives in the class's namespace ...
    (comp.lang.python)
  • Re: qualified name and unqualified name
    ... Unqualified name means no namespace qualifier ahead of a name? ... Others are unqualified (of their scope). ... understanding of "point of instantiation". ...
    (microsoft.public.vc.language)
  • Re: Where do nested functions live?
    ... class namespace(object): ... m, scope.seconds = divmod ... scope object. ... within the outer function defenition. ...
    (comp.lang.python)
  • Re: Elegant way to do this?
    ... I think the dangers of using directives are often overstated. ... I also think the danger applies primarily to code that's already ... our code should contain relatively little at a global scope -- ... global scope (i.e. outside that namespace) will NOT cause a conflict ...
    (alt.comp.lang.learn.c-cpp)