Which scope is searcheg first ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Razvan

    Which scope is searcheg first ?

    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,
  • Victor Bazarov

    #2
    Re: Which scope is searcheg first ?

    Razvan wrote:[color=blue]
    > 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.[/color]

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

    Victor

    Comment

    • Jerry Coffin

      #3
      Re: Which scope is searcheg first ?

      mihai11@mailcit y.com (Razvan) wrote in message news:<15f19d61. 0406151238.1bdf 0f17@posting.go ogle.com>...

      [ ... ]
      [color=blue]
      > 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[/color]

      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.

      Comment

      • Razvan

        #4
        Re: Which scope is searcheg first ?

        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

        [color=blue]
        > 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[/color]

        For example:

        void func ()
        {

        {
        using namespace SomeNamespaceAB C;
        func_from_names pace_ABC();
        }

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

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


        [color=blue]
        > 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[/color]

        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

        Comment

        • Fraser Ross

          #5
          Re: Which scope is searcheg first ?


          "Razvan"[color=blue]
          > void func ()
          > {
          >
          > {
          > using namespace SomeNamespaceAB C;
          > func_from_names pace_ABC();
          > }
          >
          > func_from_names pace_ABC(); // here it fails !? the namespace is
          > not in scope any more ?!
          > }
          >
          > That means the 'using' clause is valid for the current scope.
          > Right ?
          >[/color]
          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.


          Comment

          • Razvan

            #6
            Re: Which scope is searcheg first ?

            > Yeah, but Jerry spoke about a using-declaration and you give an example with[color=blue]
            > a using-directive and you have made your own term for it.[/color]

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



            Razvan

            Comment

            • Fraser Ross

              #7
              Re: Which scope is searcheg first ?

              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"[color=blue][color=green]
              > > Yeah, but Jerry spoke about a using-declaration and you give an example[/color][/color]
              with[color=blue][color=green]
              > > a using-directive and you have made your own term for it.[/color]
              >
              > What is the difference between a 'using declaration' and a
              > 'using directive' ?
              >
              >
              >
              > Razvan[/color]


              Comment

              Working...