Xirdus Xirdus - 1 year ago 81
C++ Question

Access private elements of object of same class

Is this legal? If not, will the following code allow this?

class Foo
friend class Foo;

Answer Source

That's redundant. Foo already has access to all Foo members. Two Foo objects can access each other's members.

class Foo {
  int touchOtherParts(const Foo &foo) {return foo.privateparts;}
  int privateparts;

Foo a,b;

The above code will work just fine. B will access a's private data member.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download