Thread: Question about scope

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    412

    Question about scope

    I've recently begun reading the "dragon book" (Compilers: Principles, Techniques, and Tools - Wikipedia, the free encyclopedia) and where the authors mentions scope they say this:
    Most languages, including C and its family, use static scope. The scope rules for C are based on program structure; the scope of a declaration is determined implicitly by where the declaration appears in the program. Later languages, such as C++, Java, and C#, also provide explicit control over scopes through the use of keywords like public, private, and protected.
    But I don't understand what they mean by using public/protected/private to control the scope and I can't find an explanation for it in the book.
    Consider the following code:
    Code:
    class A
    {
     public:
      int i;
     private:
      int j;
    }
    
    A a;
    a.i = 0; // valid
    a.j = 0; // illegal
    Now, I obviously can't access j outside of A. But both i and j have the same lifetime, and I've been taught that scope and lifetime are basically just different words for the same thing.
    Is there something I'm missing here?
    Last edited by _Mike; 07-08-2011 at 11:01 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about scope...
    By CommonTater in forum C++ Programming
    Replies: 3
    Last Post: 01-14-2011, 05:05 AM
  2. Question on scope
    By Kiklion in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2009, 03:15 AM
  3. Question About Scope
    By Kernel Sanders in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2008, 07:09 PM
  4. Scope Question
    By Vylrath in forum C++ Programming
    Replies: 9
    Last Post: 03-06-2006, 02:53 PM
  5. Scope question
    By mikebrewsj in forum C++ Programming
    Replies: 1
    Last Post: 01-17-2002, 04:47 PM