-
February 16th, 2009, 02:45 AM
#1
what is visibility and scope
What is visibility and scope? what are difference between them?
-
February 16th, 2009, 04:47 AM
#2
Re: what is visibility and scope
'scope' usually refers to the lifetime of a variable (the time between it getting created and destroyed). 'visibility' refers to whether or not it's accessible. However, it's not quite as simple as that because local variables can hide variables of the same name that were previously visible and in scope. For example:-
Code:
class myClass
{
public:
myClass(); // c'tor
int publicInt;
private:
int privateInt;
};
// Somewhere in your code
myClass myObj;
myObj.publicInt = 3; // Acceptable - 'publicInt' is public - therefore visible
myObj.privateInt = 3; // 'privateInt' is private - it remains in scope for the duration of 'myObj' but is not visible from outside the object
// Another example
myClass::SomeFunc()
{
int x;
publicInt = 9; // refers to the member variable
if (something_or_other)
{
int publicInt = 3; // This new declaration hides the member variable temporarily
// Within the scope of these 2 brackets, 'publicInt' refers to the
// local declaration. Member 'publicInt' is temporarily hidden.
x = publicInt; // 'x' will equal 3
}
x = publicInt; // 'x' will equal 9
}
"A problem well stated is a problem half solved.” - Charles F. Kettering
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|