tags:

views:

65

answers:

3

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 attempt:

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?

+2  A: 

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)

Kornel Kisielewicz
Duh. Figured I was missing something obvious. Thanks for pointing it out.
drby
@drby: note however, that David's answer still stands -- it's not allowed in the standard (I take his word for it) so it may not be portable between compilers.
Kornel Kisielewicz
+2  A: 

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.

David Rodríguez - dribeas
A: 

You should probably make

void privateFunction(){}

protected.

Ooops, I forgot you cannot modify Foo. No other way to do it, afaik.

Iustin Amihaesei