so by making friend declarations I can access private variables from from other classes in the class im working on?
|
Yes you can, but just because you can, doesn't mean you should.
This is why I used the mathematical notation "IFF" (if and only if). This is not something you should be doing, and if you find yourself needing to access private data in this way, I suggest re-thinking your approach.
You've made your data private for a reason, if you need to access it, (for example) create 2 public member functions for each data member, one to get the data and the other to set it, and call these functions. I know it's a hassle at first, but it's worth it in the long run.
(Better yet) instead of getting and setting, why not create member functions that achieve your ultimate goal? That way a class manages it's own internal data and you're free to change it's implementation without necessarily having to touch your interface and upsetting the users of your class. For (contrived) example:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
class Door
{
public:
Door() : shut(true), locked(true) {}
void Open() { locked = false; shut = false; }
void Shut() { locked = false; shut = true; }
void Lock() { shut = true; locked = false; }
void Unlock() { shut = true; locked = true; }
private:
bool shut;
bool locked;
};
|
It's a silly example, but the point I want to make is that instead of functions getting and setting the values of "shut" and "locked", I have functions that do what I really want, i.e Close() or Open() and it is up to these functions to manipulate the internal data as they see fit. This way, I can change how a door is Lock()ed or Open()ed, independently of how the calls are made.
There are arguably good uses of friend (e.g. overloading non-member operators), but many of us just abuse the facility in order to save some extra typing