Friend mixin template?
Let's say I have two classes Foo and Bar, and I want to make Foo friends with Bar without changing Foo. Here's my at开发者_高级运维tempt:
class Foo
{
public:
Foo(){}
private:
void privateFunction(){}
};
template <class friendly, class newFriend>
class friends : public friendly
{
private:
friend newFriend;
};
class Bar
{
public:
Bar(){}
void callFriendlyFunction()
{
friendlyFoo.privateFunction();
}
private:
friends<Foo, Bar> friendlyFoo;
};
int main(int argc, char* argv[])
{
Bar bar;
bar.callFriendlyFunction();
return 0;
}
Getting a compiler error about trying to call a private function, so apparently it didn't work. Any ideas?
It doesn't work, because friends
has no access to privateFunction
anyway, because it's private
(descendant classes have no access to private fields anyway). If you would declare privateFunction
as protected
, it would work.
Here's a nice paper about Mixins in C++. (PDF link)
Only a class can declare who its friends are. They cannot be injected from outside. This just makes plain sense: if it was allowed by the language, it might just forget about the private keyword at all, after all any code that intended on accessing the private members could just use that trick. Note that adding the friend relationship in a derived object will not help, as the method is not accessible from the derived template.
Any other approach you can try is hackery and non-portable (rewritting the same header changing the private
for public
will seem to work in many situations, but it will fail in some corner cases).
Also note that within a class template you cannot declare a type-argument as friend, it is explicitly prohibited in the current standard, even if that limitation will be removed in the upcoming standard.
You should probably make
void privateFunction(){}
protected.
Ooops, I forgot you cannot modify Foo. No other way to do it, afaik.
精彩评论