Thread: Why this code works? (reference to a class's private variable)

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    Why this code works? (reference to a class's private variable)

    Code:
    class B{                                                                                                                                                                     
    public:                                                                                                                                                                      
        B(){}                                                                                                                                                                    
        void check(const int& y){cout<<y<<endl;}                                                                                                                                 
    };                                                                                                                                                                           
                                                                                                                                                                                 
    class A{                                                                                                                                                                     
        int x;                                                                                                                                                                   
    public:                                                                                                                                                                      
        A(){x=1;}                                                                                                                                                                
        void foo(){B ob; ob.check(x);}                                                                                                                                           
    };                                                                                                                                                                           
                                                                                                                                                                                 
                                                                                                                                                                                 
    int main(){                                                                                                                                                                  
                                                                                                                                                                                 
        A oa;                                                                                                                                                                    
        oa.foo();                                                                                                                                                                
        return 0;                                                                                                                                                                
    }
    x is A's private member, how come B can refer to it (even it's a const) ?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    B doesn't refer to A's private variable. All it sees is a parameter passed into a function. A can see its own private variable, and can choose to send it wherever it wants.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    x is A's private member, how come B can refer to it (even it's a const) ?
    You are passing an argument, so the B object never actually deals with the member variable named x of A, but its check member function deals with a local variable named y. It so happens that pass by reference is used, so y becomes an alias for x when ob.check(x) is called.
    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

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    OK, To make my question more clear, I modify the code:

    Code:
    class B{                                                                                                                                                                     
    public:                                                                                                                                                                      
        B(){}                                                                                                                                                                    
        void check( int& y){ y++;}                                                                                                                                               
    };                                                                                                                                                                           
                                                                                                                                                                                 
    class A{                                                                                                                                                                     
        int x;                                                                                                                                                                   
    public:                                                                                                                                                                      
        A(){x=1;}                                                                                                                                                                
        void foo(){                                                                                                                                                              
            cout<<x<<endl;                                                                                                                                                       
            B ob;                                                                                                                                                                
            ob.check(x);                                                                                                                                                         
            cout<<x<<endl;}                                                                                                                                                      
    };                                                                                                                                                                           
                                                                                                                                                                                 
                                                                                                                                                                                 
    int main(){                                                                                                                                                                  
                                                                                                                                                                                 
        A oa;                                                                                                                                                                    
        oa.foo();                                                                                                                                                                
        return 0;                                                                                                                                                                
    }
    Now you see before( and after) the call, A's private member variable actually was changed.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, apparently class A wanted it changed, since it sent the variable off to a function pass-by-reference. The responsibility for changing the value rests right there, not in class B.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Thanks. But x is A's private variable, how would it be changed by a non-friend function?

    Quote Originally Posted by tabstop View Post
    Well, apparently class A wanted it changed, since it sent the variable off to a function pass-by-reference. The responsibility for changing the value rests right there, not in class B.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    If you pass a value by reference it does NOT matter who owns it, what class it is in. If you pass a variable by reference it is able to be changed by the function regardless of the ownership.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    "Private" means "Nobody can see this name," not "Nobody can touch this variable."

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    But x is A's private variable, how would it be changed by a non-friend function?
    I think you have a fundamental misunderstanding of scope and access control.

    Let's ask a few questions and see how you answer them:
    Code:
    void foo(int& x)
    {
        x = 0;
    }
    
    void bar()
    {
        int y = 1;
        foo(y);
    }
    
    class Z
    {
    private:
        void baz()
        {
            int z = 2;
            foo(z);
        }
    };
    1. Is there a local variable named y in foo(int&)?
    2. Is there a local variable named z in foo(int&)?
    3. Remove the call of foo(y) in bar(). Now, without making any further modification to bar(), can you modify foo(int&) such that it changes the value of y in bar()?
    4. Remove the call of foo(z) in Z::baz(). Now, without making any further modification to Z::baz(), can you modify foo(int&) such that it changes the value of z in Z::baz()?

    The answer to all four questions is no. So, foo(int&) does not have access to y or z. In particular, foo(int&) does not have access to z, but due to pass by reference its parameter x can be an alias (another name) for z if Z::baz() calls foo(z). So, the private access is respected since only a member function (or friend) of Z can access z.
    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

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Your ATM card and pin are private to you.
    If you choose to give me your card and pin #, what's to stop me from emptying your bank account?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  2. Question about OpenGL/Linux
    By Ideswa in forum Linux Programming
    Replies: 12
    Last Post: 09-10-2006, 05:56 AM
  3. Strange/false errors
    By Ganoosh in forum Windows Programming
    Replies: 8
    Last Post: 10-20-2005, 04:54 PM
  4. Textbox
    By maxorator in forum Windows Programming
    Replies: 20
    Last Post: 09-25-2005, 10:04 AM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM