How can I remove/refactor a «friend» dependency declaration properly?

Let’s setup some constraints for refactoring first: The ClassAAccessor’s publicly visible interface should change in no way The ClassA internal operations should not be visible/accessible from the public The overall performance and footprint of the original design should not be hurt Step 1: Introduce an abstract interface For a first shot, I factored out the … Read more

Access friend function defined in class

class A{ public: friend void fun(A a){std::cout << “Im here” << std::endl;} friend void fun2(){ std::cout << “Im here2″ << std::endl; } friend void fun3(); }; Although your definition of fun2 does define a “global” function rather than a member, and makes it a friend of A at the same time, you are still missing … Read more

Why does C# not provide the C++ style ‘friend’ keyword? [closed]

On a side note. Using friend is not about violating the encapsulation, but on the contrary it’s about enforcing it. Like accessors+mutators, operators overloading, public inheritance, downcasting, etc., it’s often misused, but it does not mean the keyword has no, or worse, a bad purpose. See Konrad Rudolph‘s message in the other thread, or if … Read more

What’s the scope of inline friend functions?

When you declare a friend function with an unqualified id in a class it names a function in the nearest enclosing namespace scope. If that function hasn’t previously been declared then the friend declaration doesn’t make that function visible in that scope for normal lookup. It does make the declared function visible to argument-dependent lookup. … Read more

Can we increase the re-usability of this key-oriented access-protection pattern?

I like this idiom, and it has the potential to become much cleaner and more expressive. In standard C++03, I think the following way is the easiest to use and most generic. (Not too much of an improvement, though. Mostly saves on repeating yourself.) Because template parameters cannot be friends, we have to use a … Read more

When should you use ‘friend’ in C++?

Firstly (IMO) don’t listen to people who say friend is not useful. It IS useful. In many situations you will have objects with data or functionality that are not intended to be publicly available. This is particularly true of large codebases with many authors who may only be superficially familiar with different areas. There ARE … Read more