CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2008
    Posts
    16

    what is visibility and scope

    What is visibility and scope? what are difference between them?

  2. #2
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    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
  •  





Click Here to Expand Forum to Full Width

Featured