Friendship
We can use overrider containers to grant friendship to a specific overrider, or
to all the overriders of a method. The name of the container template is
returned by BOOST_OPENMETHOD_OVERRIDERS
. The template argument for a
specialization is the signature of the overrider. For example, the overrider of
poke
for Cat
is:
BOOST_OPENMETHOD_OVERRIDERS(poke)<
void(std::ostream& os, virtual_ptr<Cat> cat)>::fn;
We can thus grant friendship to all the overriders of poke
:
class Cat;
class Dog;
class Animal {
// ...
private:
std::string name;
template<typename> friend struct BOOST_OPENMETHOD_OVERRIDERS(poke);
};
Be aware, though, that the overriders of any method called poke
- with any
signature - are granted friendship.
We can also befriend individual overriders:
class Cat;
class Dog;
template<typename> struct BOOST_OPENMETHOD_OVERRIDERS(poke);
class Animal {
// ...
private:
std::string name;
friend struct BOOST_OPENMETHOD_OVERRIDERS(poke)<void(std::ostream&, virtual_ptr<Cat>)>;
friend struct BOOST_OPENMETHOD_OVERRIDERS(poke)<void(std::ostream&, virtual_ptr<Dog>)>;
};