Here is my situation (a simple example).
Say I have a class called HiddenData
Then I have another class called StoreHiddenData
And finaly a class called OperateHiddenData
Here's the thing, my class called HiddenData
has private members that I don't want to be visible to the rest of the world, but I still need classes to be able to operate on private members of this HiddenData
class (StoreHiddenData
and OperateHiddenData
for example). I would like to be able to pass this object around to different classes that do different things on it.
Another thing I was thinking of is to create a form of handle to these HiddenData
objects, but I can't think of a way to use handles without using friend classes again (as to give the other classes access to a table that contains what the handles point to, without exposing it to the rest of the world as well).
I was reading this question on this site just a while ago as I was researching the use of friend classes, and it has me thinking if using friends is the best way to go.
If anyone has any suggestions for how to accomplish this task without the use of friend classes in C++, or if the use of friend classes is an OK OOP practice, please provide me with any suggestions. Thanks.
EDIT: I figure I should add a little bit more information about my problem. For one thing, I am trying to create an API that consists of several classes that hide their functionality from the outside and only allow public functions to be accessed. On the inside I have data structures (or classes, doesn't really matter), that need to be passed around to these different classes with public functions. I don't want the outside to have access to anything inside these data structures other than being able to pass them around to functions of the API. I kind of think of it like the Windows API, where you pass around Handles to API functions, but you can't actually dissect the handles in your own code. Well I would like to do something very similar, but while remaining object oriented, and NOT passing these private data structures around by value because they will be BIG. Thanks again.
friend
in C++. For example, the Iterator design pattern can usefriend
to access private members so that the aggregate (the thing being iterated over) doesn't have to expose methods for the sole purpose of iteration. It lets you keep iteration methods in the iterator and data structure maintenance methods in the data structure. Usingfriend
to accomplish similar goals is probably a good use of the language constructs of C++.