While designing an interface for a class, I usually get caught in two brains or not For example, by using a combination of other member functions, for example:
Classroom Doscuntyear {public: Doctor * getDoc (int index) CONST; Bool Haydok selected (Doctor *) Const; Int getDocCount () console; // This method should be here ??? // This method gives the selected document in the container (in the selected docs_out) zero receive headset docs (std :: vector & lt; doc * & gt; and selected docs_out) const; };
Should I provide it as a class member function or perhaps a namespace where I can define this method? Which is the favorite? In general, think of an OOP perspective about how you should work for free, about the
.
If the function does not need access to a private member, then why should it use given ? It is not good for encapsulation
This code also restricts the possible amount of reuse.
If you type something like this in the function:
template & lt; Typename T & gt; Bool getSelectedDocs (T & container, std :: vector & lt; Doc * & gt; & amp;;);
The only implementation of the dose-selected docs will work for any class that highlights the required functions, not just your doctor
Of course, if you do not like the template, an interface can be used, and after that it will still work for any class that implements this interface.
On the other hand, if this is a member function, then it will work only for this special category (and potentially derived class).
The C ++ standard libraries follow the same approach, for example, consider std :: find
, which works a free for this exact cause. It does not need to know the interior of the inner search class. It only requires implementation of some which meets its requirements. This means that the same search ()
implementation can work on any container, standard library or elsewhere.
There is logic for Scott Meyer.
If you do not like it cluttering your main namespace, you can of course put it in a different namespace with functionality for this particular category.
Comments
Post a Comment