Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Visibility vs Scope

Reply
Thread Tools

Visibility vs Scope

 
 
goodbadwolf
Guest
Posts: n/a
 
      03-24-2008
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?
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-24-2008
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


 
Reply With Quote
 
 
 
 
notobel
Guest
Posts: n/a
 
      03-24-2008
On 24 Mrz., 17:52, "Victor Bazarov" <(E-Mail Removed)> 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
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      03-24-2008
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
 
Reply With Quote
 
Erik Wikström
Guest
Posts: n/a
 
      03-24-2008
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
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      03-24-2008
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
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      03-24-2008
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...)
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      03-24-2008
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
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      03-25-2008
On Mar 24, 5:06 pm, goodbadwolf <(E-Mail Removed)> 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:(E-Mail Removed)
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
visibility, declarative region, scope, validity, introduction and accessibility Steven T. Hatton C++ 1 12-13-2006 06:29 AM
Scope visibility Kaba C++ 3 03-14-2006 03:31 AM
Variable Scope / Visibility / Caching Problem Steve Wark ASP .Net 1 05-23-2005 12:36 PM
Variable Scope / Visibility Steve Wark ASP .Net 0 05-23-2005 12:00 PM
Is "scope" different from "visibility" ? TTroy C Programming 8 02-14-2005 09:46 AM



Advertisments