Saturday, 30 July 2011

Friend functions

So how are we going to improve "friend" for "DeadMG++"?

Well, firstly, we are going to add a "friend" accessibility level, and I'm thinking about making it so that you can make multiple friend levels. I think that this will make "friend" more controllable.

Secondly, I'm going to introduce friendship transferring.

Have you ever tried to friend a Standard function so that it could use your functionality? It probably gave an error anyway, because when you call a Standard C++ function, there's no guarantee that that function actually does the work. Maybe it delegates to some other functions that do it. In this case, it's violating the abstraction, because even though you friended the function that was supposed to do the work, it still doesn't work because you have to friend the function that needs it. I had this problem with private constructors and make_shared in Visual Studio.

This is what friendship transfers are for. Effectively, you can specify individual expressions or individual objects to be friended, rather than necessarily whole classes or functions. And secondly, you can "pass on" your access level, by specifying a function call or object to be a friend- even if you're a free function. This means that if you use helper functions to do your real work, then you can give them your access, hiding their existence from anyone who tried to friend you to get work done.

This should allow more fine-grained and more abstract use of friend than exists in C++.

No comments:

Post a Comment