Thread: help understanding object scope in C++

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    13

    help understanding object scope in C++

    I am getting started on C++ and self educating myself from a text book. I am kind of confused at one particular point in the introduction to access specifiers.

    the author starts off saying private members cannot be accessed directly by objects of a class and they can only be accessed by member functions. I am copy pasting the example he used in the book.

    Code:
    class clockType
    {
    public:
    
    bool equalTime(const clockType& otherClock) const;
    //Function to compare the two times
    //Postcondition: Returns true if this time is equal to
    // otherClock; otherwise, returns false
    
    private:
    
    int hr; //stores the hours
    int min; //store the minutes
    int sec; //store the seconds
    };
    
    
    
    bool clockType::equalTime(const clockType& otherClock) const
    {
    return (hr == otherClock.hr
    && min == otherClock.min
    && sec == otherClock.sec);
    }
    In this example, the equalTime() function is using otherClock.hr, otherClock.min etc which is a direct access to private variables of the class object otherClock. I couldn't quite understand his explanation for this. I understand it is from inside a member function but it is another object's private variable directly being accessed here. Can anyone please clarify.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by livin4th
    the author starts off saying private members cannot be accessed directly by objects of a class and they can only be accessed by member functions.
    Perhaps the author meant "objects of other classes". More accurately, if a member of a class is private, then "its name can be used only by members and friends of the class in which it is declared".

    Quote Originally Posted by livin4th
    In this example, the equalTime() function is using otherClock.hr, otherClock.min etc which is a direct access to private variables of the class object otherClock. I couldn't quite understand his explanation for this. I understand it is from inside a member function but it is another object's private variable directly being accessed here. Can anyone please clarify.
    The rationale for having the notion of private access is so that the author of a class can indicate to users of the class that "these are implementation details, don't touch them, and if you do, the compiler will stop and complain". This then allows a future maintainer of the class to change the implementation details without requiring users of the class to change their code (though they may need to recompile).

    Who will implement or change the equalTime member function of clockType? Obviously, it is the author or a maintainer of the clockType class. Therefore, it is silly to say that "you cannot access otherClock's private members from equalTime because it is a different object!" because in the end, if the implementation details of clockType changes, it changes for both the current object and for otherClock, hence from within the definition of equalTime, it is fine to access the private members of otherClock.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > private members cannot be accessed directly by objects of a class
    Which is basically wrong.

    Access to private member variables is ordinarily only via member functions (and friend functions). The functions themselves are not bound to specific instances of the class (you don't get a new copy of the code each time you create an instance of the object).

    Anything declared outside the class has to go through the public interface declared for the class.

    Your clockType::equalTime can be viewed as
    bool clockType::equalTime(clockType *this, const clockType& otherClock) const
    Every non-static member function gets 'this' pointer for free, and hr == otherClock.hr is really this->hr == otherClock.hr

    You can of course subvert this by writing member functions which return pointers or references to member variables.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Smalltalk disallows objects of the same type from fiddling with each others' private parts. So sad.
    Last edited by algorism; 04-19-2016 at 06:51 AM.

  5. #5
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by algorism View Post
    Smalltalk disallows objects of the same type from fiddling with each others' private parts. So sad.
    Am guessing this applies to C++ too except may be the part where you have static member, wait, can static variables even be private ?

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by Aslaville View Post
    Am guessing this applies to C++ too except may be the part where you have static member, wait, can static variables even be private ?
    Actually, the topic of this thread is that it doesn't apply to C++, i.e., that the "private boundary" (to coin a term) is at the class level in C++, not the object level.

    I was just mentioning an OOP language where the boundary is at the object level.

    And, yes, in C++ static members can be private.
    Last edited by algorism; 04-19-2016 at 08:08 AM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by algorism
    Smalltalk disallows objects of the same type from fiddling with each others' private parts. So sad.
    Well, it makes sense from a purely theoretical object oriented message passing perspective, but it is just an unnecessary restriction from a practical development and maintenance perspective.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by laserlight View Post
    Well, it makes sense from a purely theoretical object oriented message passing perspective, but it is just an unnecessary restriction from a practical development and maintenance perspective.
    I agree. The object obviously knows all about it's class's internals so why not allow it access to the internals of objects of the same class? C++ is, as you say, above all a practical language. Smalltalk was never (as far as I know) a production language; more of a teaching/research tool. I wonder what Simula is like in this regard.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help in Understanding object return
    By deathmetal in forum C++ Programming
    Replies: 2
    Last Post: 11-25-2013, 01:58 AM
  2. Question: Determining object scope from root object
    By Imanuel in forum C++ Programming
    Replies: 6
    Last Post: 12-25-2012, 11:20 AM
  3. Replies: 7
    Last Post: 05-22-2010, 05:42 PM
  4. Object not declared in this scope
    By -EquinoX- in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2009, 01:31 AM
  5. Replies: 4
    Last Post: 11-14-2006, 11:52 AM