0

Doesn't this break encapsulation?

B.h:

class B{
    int x, y;
public:
    B() : x(1), y(1) {}
    B(const B& obj) : x(obj.x), y(obj.y) {}

    int getx();
    int gety();

    friend void pass_private_members(B&);
};

B.cpp

void non_friend_pass_private_members(int& x);

int B::getx(){
    return this->x;
}

int B::gety(){
    return this->y;
}

void pass_private_members(B& obj){
    non_friend_pass_private_members(obj.x);
}

void non_friend_pass_private_members(int& x){
    x++;
}

main.cpp

int main(){
    B obj;
    pass_private_members(obj);
    cout << obj.getx() << endl;
    return 0;
}
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
naxchange
  • 893
  • 1
  • 11
  • 24
  • No, nor would calling `strcpy()` on a `char *` member from the context of a friend function. It, likewise, is not friended, but the caller *is*. The friendship is granting the same privileges as the object. So ask yourself: Could you call `non_friend_pass_private_members()` from your *objects* member functions? – WhozCraig Mar 25 '13 at 04:26
  • It isn't clear what you mean by "breaking encapsulation". – Vaughn Cato Mar 25 '13 at 04:29
  • @VaughnCato accessing private members from non-friend and non-member functions – naxchange Mar 25 '13 at 04:30
  • You have to declare the friend inside the class. Therefore, you are in control of what exactly can access members. If you choose to pass actual members along, it's the same as if you did it inside the class. – chris Mar 25 '13 at 04:30
  • @WhozCraig I think he means accessing private variables from friend function breaks encapsulation rules(guessing here) – Aniket Inge Mar 25 '13 at 04:33

2 Answers2

3

By declaring friend void pass_private_members(B&) you are telling compiler you trust the pass_private_members to handle the private members of B. What it does with the private members is not of compiler's concern. This is no different from having a member function which calls the non_friend_pass_private_members.

Asha
  • 11,002
  • 6
  • 44
  • 66
  • 1
    having `friend` function break all rules of encapsulation. The question is "Does it break rules of encapsulation" so your answer does not reflect an 'yes' or 'no' in that regard. – Aniket Inge Mar 25 '13 at 04:38
2

yes

it does break rules of encapsulation. But C++ breaks a lot of rules of "purely" OOPL at one point or another.

Reminds me of my favorite C++ quote:

C++, the only language where friends can access your privates!

Community
  • 1
  • 1
Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
  • Ah, but according to this question, if your friends want to, they can let others do it, too – naxchange Mar 25 '13 at 04:36
  • I contend you're correct that is what he *thinks* it is doing. My assessment is that in granting friendship to a class, you grant all rights members of the class would have. Therefore, by his conjecture (and by your answer, *yours* too) *anything* not specifically granted friendship accessing member data is breaking encapsulation. This would include runtime library functions, etc. executed not only from friend functions, but *members* as well. I respectfully disagree. You're not breaking any rules where you're specifically *told* you need not follow them. – WhozCraig Mar 25 '13 at 04:41
  • @WhozCraig I appreciate your insight. :-) – Aniket Inge Mar 25 '13 at 04:42
  • And I yours. It is a good point you make. – WhozCraig Mar 25 '13 at 04:43