Visibility vs Scope

Discussion in 'C++' started by goodbadwolf, Mar 24, 2008.

  1. goodbadwolf

    goodbadwolf Guest

    goodbadwolf, Mar 24, 2008
    #1
    1. Advertising

  2. goodbadwolf wrote:
    > Is there any difference between the term "visibility" and "scope" of a
    > variable. Read http://www.eskimo.com/~scs/cclass/notes/sx4b.html
    > article. According to my understanding it says the two terms are
    > equivalent. Am I right?


    From the language (English) point of view, 'scope' is the _area_ from
    which the variable is visible, and 'visibility' is the _ability_ of
    the variable to be referred to by its name from a particular location.
    Considering that, 'scope' != 'visibility', but they are related.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Mar 24, 2008
    #2
    1. Advertising

  3. goodbadwolf

    notobel Guest

    On 24 Mrz., 17:52, "Victor Bazarov" <> wrote:
    > goodbadwolf wrote:
    > > Is there any difference between the term "visibility" and "scope" of a
    > > variable. Readhttp://www.eskimo.com/~scs/cclass/notes/sx4b.html
    > > article. According to my understanding it says the two terms are
    > > equivalent. Am I right?

    >
    > From the language (English) point of view, 'scope' is the _area_ from
    > which the variable is visible, and 'visibility' is the _ability_ of
    > the variable to be referred to by its name from a particular location.
    > Considering that, 'scope' != 'visibility', but they are related.
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask


    I'm not too familiar with English language, so have a question. If a
    name for a variable is declared in a block the scope of the name is
    from the point of declaration until the end of the block. If the block
    has a nested block, and if a variable with the same name is declared
    in it, it hides the outer entity until the end of the inner block. How
    would you define scope and visibility in this case? Still the same?

    Franz
     
    notobel, Mar 24, 2008
    #3
  4. notobel wrote:
    > ...
    > I'm not too familiar with English language, so have a question. If a
    > name for a variable is declared in a block the scope of the name is
    > from the point of declaration until the end of the block.


    That's not exactly correct. What you describe is called "potential
    scope", not just "scope".

    > If the block
    > has a nested block, and if a variable with the same name is declared
    > in it, it hides the outer entity until the end of the inner block. How
    > would you define scope and visibility in this case? Still the same?


    The difference between the "scope" and "potential scope" is that "scope"
    equals "potential scope" minus the regions where the identifier is
    hidden. Which is exactly what you seem to mean by "visibility".

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Mar 24, 2008
    #4
  5. On 2008-03-24 17:52, Victor Bazarov wrote:
    > goodbadwolf wrote:
    >> Is there any difference between the term "visibility" and "scope" of a
    >> variable. Read http://www.eskimo.com/~scs/cclass/notes/sx4b.html
    >> article. According to my understanding it says the two terms are
    >> equivalent. Am I right?

    >
    > From the language (English) point of view, 'scope' is the _area_ from
    > which the variable is visible, and 'visibility' is the _ability_ of
    > the variable to be referred to by its name from a particular location.
    > Considering that, 'scope' != 'visibility', but they are related.


    The following gives an example where scope and visibility are different:

    #include <iostream>

    class Foo {
    int i;
    public:
    Foo() : i(0) {}
    void bar() {
    int i = 1;
    std::cout << i << std::endl;
    }
    };

    int main() {
    Foo f;
    f.bar();
    }

    In bar() the member variable i is in scope but it is hidden by the local
    variable i, i.e. it is not visible in bar() (at least not after the
    declaration of the local variable). The member is still in scope and can
    be accessed by either Foo::i or this->i.


    --
    Erik Wikström
     
    Erik Wikström, Mar 24, 2008
    #5
  6. Erik Wikström wrote:
    > The following gives an example where scope and visibility are different:
    >
    > #include <iostream>
    >
    > class Foo {
    > int i;
    > public:
    > Foo() : i(0) {}
    > void bar() {
    > int i = 1;
    > std::cout << i << std::endl;
    > }
    > };
    >
    > int main() {
    > Foo f;
    > f.bar();
    > }
    >
    > In bar() the member variable i is in scope but it is hidden by the local
    > variable i,


    No, not according to the standard definition of scope (3.3) and the
    definition of class scope (3.3.6). The _potential_ scope of member
    variable 'i' extends to the body of 'bar', but the scope of member
    variable 'i' in 'bar' ends at the point where the local 'i' is declared.

    > i.e. it is not visible in bar() (at least not after the
    > declaration of the local variable).


    Neither it is in scope there.

    Being visible is still equivalent to being in scope in your example, as
    long as you stick to the standard definition of scope.

    > The member is still in scope and can
    > be accessed by either Foo::i or this->i.


    The member is in its potential scope, but not in scope. The fact that
    you can actually access it doesn't have much to do with scope.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Mar 24, 2008
    #6
  7. notobel wrote:
    > If a
    > name for a variable is declared in a block the scope of the name is
    > from the point of declaration until the end of the block.


    Maybe nitpicking, but that's not always so. For example:

    namespace foo
    {
    int i; // point of declaration
    } // end of block

    namespace foo
    {
    void foo() { i = 5; } // Ok, because i is visible here
    }

    (Maybe namespace blocks can be considered special in that they can be
    "split" into several blocks, yet they are still handled like they were
    one single block...)
     
    Juha Nieminen, Mar 24, 2008
    #7
  8. Juha Nieminen wrote:
    > notobel wrote:
    >> If a
    >> name for a variable is declared in a block the scope of the name is
    >> from the point of declaration until the end of the block.

    >
    > Maybe nitpicking, but that's not always so.


    Actually, it is. The real nitpick here is that it is the above is true
    for the so called "potential scope" of the name, not for actual "scope".

    > For example:
    >
    > namespace foo
    > {
    > int i; // point of declaration
    > } // end of block
    >
    > namespace foo
    > {
    > void foo() { i = 5; } // Ok, because i is visible here
    > }
    >
    > (Maybe namespace blocks can be considered special in that they can be
    > "split" into several blocks, yet they are still handled like they were
    > one single block...)


    "Block" in the standard terminology is a synonym for "compound
    statement". What you see in namespace definition is not a block.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Mar 24, 2008
    #8
  9. goodbadwolf

    James Kanze Guest

    On Mar 24, 5:06 pm, goodbadwolf <> wrote:
    > Is there any difference between the term "visibility" and
    > "scope" of a variable.
    > Readhttp://www.eskimo.com/~scs/cclass/notes/sx4b.html article.
    > According to my understanding it says the two terms are
    > equivalent. Am I right?


    Well, the most obvious distinction here is that in C++, scope is
    defined by the C++ standard (section 3.3). Visibility isn't, so
    presumably, it can be used to mean whatever you want it to mean.
    Generally, however, I would understand visibility to mean that
    the name can be found by some form of name lookup (section 3.4
    of the standard). For example:

    namespace Toto {
    int i ;
    }

    void
    f()
    {
    // i not in "scope" here, but since I can write
    // Toto::i, it's visible?
    }

    One could argue, however, that in this case, you've specified a
    scope, and the variable is visible because of the specified
    scope.

    Two more interesting cases concern ADL and using directives. In
    the first, the compiler automatically makes certain scopes
    "visible", e.g.:

    namespace Toto {
    class Titi {} ;
    void f( Titi const& ) ;
    }

    void
    g()
    {
    Toto::Titi tata ;
    f( tata ) ; // finds Toto::f
    }

    In the second, the standard seems to make sort of a distinction
    itself: "A using-directive specifies that the names in the
    nominated namespace can be used in the scope in which the
    usingdirective appears after the using-directive. During
    unqualified name lookup (3.4.1), the names appear as if they
    were declared in the nearest enclosing namespace which contains
    both the using-directive and the nominated namespace." (The use
    of the words "as if" suggests that the names aren't in this
    scope, but will be considered "as if" they were for the purposes
    of visibility.)

    Generally speaking, however, I'd say that the main difference is
    one of point of view: scope is considered from the point of view
    of the declaration (the declaration is valid where), visibility
    from the point of view of use (is the declaration visible at
    this point). In a lot of cases, the visibility is determined by
    the scope of the declaration being considered.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Mar 25, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Wark
    Replies:
    1
    Views:
    614
    Brock Allen
    May 23, 2005
  2. Steve Wark

    Variable Scope / Visibility

    Steve Wark, May 23, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    479
    Steve Wark
    May 23, 2005
  3. TTroy

    Is "scope" different from "visibility" ?

    TTroy, Feb 10, 2005, in forum: C Programming
    Replies:
    8
    Views:
    332
    Dave Thompson
    Feb 14, 2005
  4. Kaba

    Scope visibility

    Kaba, Mar 13, 2006, in forum: C++
    Replies:
    3
    Views:
    365
    Greg Comeau
    Mar 14, 2006
  5. Steven T. Hatton
    Replies:
    1
    Views:
    429
    Steven T. Hatton
    Dec 13, 2006
Loading...

Share This Page