Visibility vs Scope

V

Victor Bazarov

goodbadwolf said:
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
 
N

notobel

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

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
 
A

Andrey Tarasevich

notobel said:
...
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".
 
E

Erik Wikström

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

Andrey Tarasevich

Erik said:
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.
 
J

Juha Nieminen

notobel said:
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...)
 
A

Andrey Tarasevich

Juha said:
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.
 
J

James Kanze

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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top