Like Tree1Likes
  • 1 Post By laserlight

Object to Object Access

This is a discussion on Object to Object Access within the C++ Programming forums, part of the General Programming Boards category; Suppose I have the following code : Code: #include<iostream> class A { int x; public : void input () { ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    14

    Object to Object Access

    Suppose I have the following code :
    Code:
    #include<iostream>
    
    class A
    { 
    int x;
    public :
    void input ()
    { std::cin>>x;
    }
    
    void add( A obj )
    { 
    x=x+obj.x;
    std::cout<<x;
    std::cout<<++obj.x;
    }
       };
    
    int main()
    {  A obj1,obj2;
    obj1.input();
    
    obj2.input();
    
    obj1.add(obj2);
    return 0;
    }
    This code executes properly , but my question is , If Obj2.x is a PRIVATE data member , then how is a Member function of Obj1 able to access obj2.x and manipulate it.

    Is there a pre enabled or friend compliance between objects of the same class?
    Last edited by Salem; 09-05-2011 at 06:45 AM. Reason: remove pointless /b/i from the code

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,301
    The private attribute of class members apply on a class-wide basis, not a per-object basis. All member functions (and friends) of class A can access any private members of any instance of A (as well as any private static members of class A). If that wasn't true, your code would not even compile.

    The catch is that, in the call obj1.add(obj2), obj2 is being passed by value to add(). That means the add() function sees a copy of obj2, not obj2 itself. It therefore changes the copy, not the original. The copy is destroyed immediately after the function returns, and data is not copied back into obj2 inside main().

    If you want the change of obj.x in the add() to be visible to the caller - i.e. for obj1.add(obj2) to directly modify obj2 - then change the add function so it accepts a reference argument.
    Code:
        void add( A &obj )   // etc
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    Indentation for the code could certainly be improved. Indentation is important.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    14
    Quote Originally Posted by Elysia View Post
    Indentation for the code could certainly be improved. Indentation is important.
    Will take of it next time
    Quote Originally Posted by grumpy View Post
    The private attribute of class members apply on a class-wide basis, not a per-object basis. All member functions (and friends) of class A can access any private members of any instance of A (as well as any private static members of class A). If that wasn't true, your code would not even compile.

    The catch is that, in the call obj1.add(obj2), obj2 is being passed by value to add(). That means the add() function sees a copy of obj2, not obj2 itself. It therefore changes the copy, not the original. The copy is destroyed immediately after the function returns, and data is not copied back into obj2 inside main().

    If you want the change of obj.x in the add() to be visible to the caller - i.e. for obj1.add(obj2) to directly modify obj2 - then change the add function so it accepts a reference argument.
    Code:
        void add( A &obj )   // etc

    What about a user-defined copy constructor then?

    Code:
     class A
          {  .......
             public: 
              A (const A &obj )
                {  x=obj.x;  
                                  }
            };

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    What exactly is your question? A copy constructor shall (in most cases) not modify whatever it's copying. In the code you've posted, it cannot make any changes to obj since it's const.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    14
    Quote Originally Posted by Elysia View Post
    What exactly is your question? A copy constructor shall (in most cases) not modify whatever it's copying. In the code you've posted, it cannot make any changes to obj since it's const.
    All Im asking is that , in case of a copy constructor , the action is completed on the basis that private data member's of an object of a class A are accessible by other objects of that class, am I right in saying this?

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    Yes, that's right. All instances of A can access the private members of all other instances of A.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    14
    Quote Originally Posted by Elysia View Post
    Yes, that's right. All instances of A can access the private members of all other instances of A.
    You might be annoyed , but I want to know why such a setting , was it planned to allow this? ( Obviously I don't expect you to get inside the mind of Bjarne ) of
    Doesn't this conflict with access specifier's and there sole purpose of protecting data?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    Quote Originally Posted by Arpan Das
    I want to know why such a setting , was it planned to allow this? (...) Doesn't this conflict with access specifier's and there sole purpose of protecting data?
    It does not conflict. Think about why access control exists in the first place. The idea is that some things should be restricted to the implementation because they are implementation detail that is more subject to change than an interface provided for use. This restriction could be established by convention, but access control establishes the restriction as a language feature.

    Now, if you are writing the implementation of a member function, you have access to the restricted part of the current object, because you need it to implement. Would there be a problem allowing you access to the restricted part of another object of the class from within the member function? Of course not, because within that context you are dealing with the same implementation detail.
    King Mir likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-07-2010, 12:35 AM
  2. Making child object access parent - best design?
    By IceDane in forum C++ Programming
    Replies: 8
    Last Post: 09-17-2009, 06:48 AM
  3. Replies: 4
    Last Post: 11-14-2006, 10:52 AM
  4. Undeclared Identifier when trying to access an object
    By tineras in forum C++ Programming
    Replies: 5
    Last Post: 05-09-2006, 02:34 AM

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