0

I have class like below in VC++:

class Student{
int id;
char *name;
public:
Student(){}

int compare(Student s){
 return strcmp(name, s.name)
}

}

I feel so strange, I can access direct to "name" of "s" without getter/setter, is there something wrong?

Andiana
  • 1,912
  • 5
  • 37
  • 73
  • possible duplicate of [With a private modifier, why can the member in other objects be accessed directly?](http://stackoverflow.com/questions/7396846/with-a-private-modifier-why-can-the-member-in-other-objects-be-accessed-directl) – T.J. Crowder Dec 28 '14 at 16:48

1 Answers1

1

compare is part of Student, of course it can access the private members of Student. Privacy applies to things outside of Student, not inside it.

If you're wondering why you can access name on s, it's because s is still a Student. Privacy is not instance-specific, it applies to the class. Code that has access to private information within Student has access to that information within all instances of Student, not just the one it's attached to.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875