Like Tree4Likes
  • 1 Post By AndrewHunter
  • 1 Post By AndrewHunter
  • 1 Post By grumpy
  • 1 Post By King Mir

Question about scope

This is a discussion on Question about scope within the C++ Programming forums, part of the General Programming Boards category; I've recently begun reading the "dragon book" ( Compilers: Principles, Techniques, and Tools - Wikipedia, the free encyclopedia ) and ...

  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-09-2011 at 12:01 AM.

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Scope is also used in C++ to refer to the visibility of a name, which is what you are demonstrating. Here is an interesting read.
    _Mike likes this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Oh I see. I was taught that public/private modifiers were called "access levels" and are not related to scope.
    I guess I have to track down my old high school teacher and give him a proper beating
    So going by that link it is correct to say that scope is related to lifetime, but not equal to it?

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Scope is a higher level language concept that can be related to the lifetime of a variable/object but that is also used to define visibility, which is what you teacher was referring to by "access levels". Short answer is....yes.
    _Mike likes this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,531
    Scope - from a computer science perspective - is an enclosing context with which values and expressions are associated.

    By that definition, a class (or struct) provides a scope for its members (whether data members, or member functions). For example, data members of a structure cease to exist when the enclosing structure ceases to exist.

    Similarly, in C and C++, a function or a block (indicated by curly braces) provides a scope for the variables declared within it. So variables cease to exist when at the closing curly brace.

    The public, protected, and private keywords in C++ affect access of members (variables and functions) from outside the scope that defines them. The defining scope (the class) contains the members. The private keyword specifies that those members cannot be accessed from outside the scope, and the protected keyword says those members can be accessed by member functions in the scope of a derived class.

    It is debatable whether those keywords affect scope, as they affect access of data members outside their defining scope but do not affect the lifetime of those data members. For example, any function can access any public member of a class if it has visibility of both the class definition and the instance of that class, but there is no linkage between the lifetime of every object and that function. The access is changed by making a data member private or protected, but the lifetime of that member is unaffected.
    _Mike likes this.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Lifetime is quite separate from scope. Lifetime refers to the time between a variable's or object's construction and destruction. Any instantiated object has a lifetime. Scope refers to what can be accessed at a given location of code. Scope is a property not of objects, but only of identifiers. A type name, a named variable and a number of other things have scope. In some cases, for variables, scope dictates lifetime. For instance curly braces in functions limit the scope and lifetime of variables declared between them. Other times it doesn't. For instance anytime a destructor is explicitly called, an object's lifetime ends, even if that object is a named variable. The variable then remains in scope and must be reconstructed. (Not reconstructing such a variable leads to undefined behavior, excepting for a few operations which allow jumping out of particular scope without stack unwinding).
    _Mike likes this.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,531
    Quote Originally Posted by King Mir View Post
    Lifetime is quite separate from scope.
    Except that the lifetime of objects is often defined by some enclosing scope.

    Lifetime (or, more generally, object state) is an important part of the context of any object.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Thanks guys. You've taught me a lot of useful information here and I have a much clearer picture now on the definition of scope.

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, 04:15 AM
  3. Question About Scope
    By Kernel Sanders in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2008, 08: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21