c++: Why the member functions of one class can access to private data member of other object? I use vs2008 and g++ compiler. the codes are list below:

#include <iostream>
using namespace std;

class Integer {
  int i;
public:
  Integer(int ii) : i(ii) {}
  const Integer
  operator+(const Integer& rv) const {
    cout << "operator+" << endl;
    return Integer(i + rv.i /*? rv.i access to the private data member*/);
  }
  Integer&
  operator+=(const Integer& rv) {
    cout << "operator+=" << endl;
    i += rv.i; //? rv.i access to the private data member
	
    return *this;
  }
  void combine( Integer& rv)
  {
	  rv.i = 2;//?rv.i access to the private data member
  }

};

int main() {
  cout << "built-in types:" << endl;
  int i = 1, j = 2, k = 3;
  k += i + j;
  cout << "user-defined types:" << endl;
  Integer ii(1), jj(2), kk(3);
  kk += ii + jj;
  
} ///:~

The private member data of other object is followed by "//?" comment lines.

I have view the thread http://www.daniweb.com/forums/thread1460.html but it seems not helpful.

Thanks in advance!

Recommended Answers

All 6 Replies

It works as its supposed to work. rv.i is accessing the private member of its own object.

It works as its supposed to work. rv.i is accessing the private member of its own object.

Could I suppose this is the rules of c++?
If so, could you give some clues under what the conditions the rules work?

Or can you give me reference so that I can read that?

Thanks

Could I suppose this is the rules of c++?

Yes. The methods of a class can see everything in the class regardless of the access level. That is because a class can always see its own privatemost stuff. This is all at the class level, not the object level. When classes are instantiated into objects, the objects follow the rules of the class.

It is the same way with friends. When you declare a friend class or function, that class or function can see everything regardless of which object you give it.

Yes. The methods of a class can see everything in the class regardless of the access level. That is because a class can always see its own privatemost stuff. This is all at the class level, not the object level. When classes are instantiated into objects, the objects follow the rules of the class.

It is the same way with friends. When you declare a friend class or function, that class or function can see everything regardless of which object you give it.

Thank you Tom!
I wanted to know whether it is the rules of C++ because I have not seen any mentions in several books like "Teaching yourself c++ in 21 days", "Thinking in C++" etc.

The rules change slightly in derived classes -- derived classes can not access private members of its parent class. Also non-class functions can access only public members of a class

class parent
{
private:
   int x;
protected:
   int y;
public:
   int z;

   parent()
   {
       x = y = z = 0; // ok
   }
};

class child : public parent
{
public:
    child()
    {
        y = z = 1; // ok
        x = 2; // illegal (private)
    }
};

int main()
{
    child c;
    c.z = 3; // ok
    c.y = 4; // illegal (protected)
    c.x = 4; // illegal (private)
}

Thanks for all the replies.

I understand the class can access to all level members of the same class just like a friend class.

I also find a similar post from LinuxQuestions

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.