Thread: How could the object access its private data members from outside?

  1. #1
    Registered User
    Join Date
    Jul 2012
    Location
    Ankara
    Posts
    52

    Arrow How could the object access its private data members from outside?

    Hi,
    I want to ask you that how does an object access its private data members in copy constructor.


    The relevant part of the code:
    Code:
    C::C(const C &obj)
    {
        x = obj.x;
        y = obj.y;
    }


    Normally the object1 called "obj" cannot access its private data members outside. But in this situation it can access. How can it be explained?


    Here are the complete code:
    Code:
    #include <iostream>
    
    
    
    
    using namespace std;
    
    
    
    
    class C{
        public:
            C(int,int);
            C(const C &);
            int area();
        private:
            int x;
            int y;
    };
    
    
    
    
    C::C(int a,int b)
    {
        x = a;
        y = b;
    }
    
    
    
    
    C::C(const C &obj)
    {
        x = obj.x;
        y = obj.y;
    }
    
    
    
    
    int C::area () {
        return x*y;
    }
    
    
    
    
    int main()
    {
        C object1(2,3);
        C object2(object1);
    
    
    
    
        cout << "Object 1 : " << object1.area() << endl;
        cout << "Object 2 : " << object2.area() << endl;
    
    
    
    
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by hefese
    Normally the object1 called "obj" cannot access its private data members outside. But in this situation it can access. How can it be explained?
    It is permitted because this access is done within a member function of the class. After all, if you are writing the core implementation of the class, then it is to be expected that you will access the implementation details directly.

    By the way, you should use the initialiser list, e.g.,
    Code:
    C::C(const C &obj) : x(obj.x), y(obj.y) {}
    But then you don't even need to declare the copy constructor here since the compiler generated one will suffice.
    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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    Jul 2012
    Location
    Ankara
    Posts
    52

    Question

    Quote Originally Posted by laserlight View Post
    It is permitted because this access is done within a member function of the class.
    I don't understand this point: This access is done within a member function of object2, not member function of object1. Because of this my mind is confusing.

    After the process of object2(object1), the copy constructor of object2 is called, isnt it? So data members of object2 can be used by object2 in copy constructor, but how do data members of object1 in this scope(in object2 scope) can access its data members. This scope is outside for object1, isnt it?

    Quote Originally Posted by Elysia View Post
    Sorry, I don't understand why you have shared this link.
    Last edited by hefese; 11-13-2013 at 03:47 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by hefese
    This access is done within a member function of object2, not member function of object1.
    Both object2 and object1 are instances of the same class, therefore there is no problem.

    Quote Originally Posted by hefese
    After the process of object2(object1), the copy constructor of object2 is called, isnt it? So data members of object2 can use in copy constructor, but data members of object1 in this scope(in object2 scope) can access its data members. How?
    Because it is permitted. It is as simple as that. The idea here is that access control is used to restrict access to implementation detail. Therefore, we are not concerned with the fact that the copy constructor of object2, not object1, is invoked here: either way, the copy constructor of the class is invoked, hence the private members of objects of the class can be accessed so as to implement the copy constructor.
    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

  6. #6
    Registered User
    Join Date
    Jul 2012
    Location
    Ankara
    Posts
    52
    I understand now, thank you for responding.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by hefese View Post
    Sorry, I don't understand why you have shared this link.
    Because you are doing this:

    C(int,int);
    C(const C &);

    (i.e. removing the name of the parameters.)
    This is exactly what the article is asking you not to do.
    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
    Jul 2012
    Location
    Ankara
    Posts
    52
    Okay, thanks for your reply.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing private Data members
    By Emeighty in forum C++ Programming
    Replies: 17
    Last Post: 08-14-2008, 11:16 PM
  2. still confused with constructors and private data members
    By freddyvorhees in forum C++ Programming
    Replies: 2
    Last Post: 07-26-2008, 09:29 AM
  3. Replies: 10
    Last Post: 07-26-2008, 08:44 AM
  4. Not able to access private data members
    By smitsky in forum C++ Programming
    Replies: 31
    Last Post: 05-09-2004, 07:06 PM
  5. Accessing private data members
    By maloy in forum C++ Programming
    Replies: 11
    Last Post: 10-04-2002, 02:48 PM

Tags for this Thread