friend

What's the scope of inline friend functions?

倖福魔咒の 提交于 2019-11-26 03:29:11
问题 After searching aroung SO, one question taught me that the lexical scope of an inline friend function is the class it\'s defined in, meaning it can access e.g. the typedef s in the class without qualifying them. But then I wondered what is the actual scope of such a function? GCC at least rejects all my attempts to call it. Can a function such as in the example ever be called through means other than ADL, which is not possible here thanks to no arguments? Standard quotations are appreciated,

Is there a way to simulate the C++ 'friend' concept in Java?

血红的双手。 提交于 2019-11-26 03:19:44
问题 I would like to be able to write a Java class in one package which can access non-public methods of a class in another package without having to make it a subclass of the other class. Is this possible? 回答1: Here is a small trick that I use in JAVA to replicate C++ friend mechanism. Lets say I have a class Romeo and another class Juliet . They are in different packages (family) for hatred reasons. Romeo wants to cuddle Juliet and Juliet wants to only let Romeo cuddle her. In C++, Juliet would

Is this key-oriented access-protection pattern a known idiom?

99封情书 提交于 2019-11-26 03:07:08
问题 Matthieu M. brought up a pattern for access-protection in this answer that i\'d seen before, but never conciously considered a pattern: class SomeKey { friend class Foo; SomeKey() {} // possibly make it non-copyable too }; class Bar { public: void protectedMethod(SomeKey); }; Here only a friend of the key class has access to protectedMethod() : class Foo { void do_stuff(Bar& b) { b.protectedMethod(SomeKey()); // fine, Foo is friend of SomeKey } }; class Baz { void do_stuff(Bar& b) { b

Access friend function defined in class

吃可爱长大的小学妹 提交于 2019-11-26 02:56:58
问题 There is such code: #include <iostream> 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(); }; void fun3(){ std::cout << \"Im here3\" << std::endl; } int main() { fun(A()); // works ok //fun2(); error: \'fun2\' was not declared in this scope //A::fun2(); error: \'fun2\' is not a member of \'A\' fun3(); // works ok } How to access function fun2()? 回答1: class A{ public: friend void fun(A

Why does C# not provide the C++ style &#39;friend&#39; keyword? [closed]

余生长醉 提交于 2019-11-26 02:18:29
问题 The C++ friend keyword allows a class A to designate class B as its friend. This allows Class B to access the private / protected members of class A . I\'ve never read anything as to why this was left out of C# (and VB.NET). Most answers to this earlier StackOverflow question seem to be saying it is a useful part of C++ and there are good reasons to use it. In my experience I\'d have to agree. Another question seems to me to be really asking how to do something similar to friend in a C#

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

寵の児 提交于 2019-11-26 01:07:26
问题 Can we increase the re-usability for this key-oriented access-protection pattern: class SomeKey { friend class Foo; // more friends... ? SomeKey() {} // possibly non-copyable too }; class Bar { public: void protectedMethod(SomeKey); // only friends of SomeKey have access }; To avoid continued misunderstandings, this pattern is different from the Attorney-Client idiom: It can be more concise than Attorney-Client (as it doesn\'t involve proxying through a third class) It can allow delegation of

When should you use &#39;friend&#39; in C++?

≡放荡痞女 提交于 2019-11-26 00:18:48
问题 I have been reading through the C++ FAQ and was curious about the friend declaration. I personally have never used it, however I am interested in exploring the language. What is a good example of using friend ? Reading the FAQ a bit longer I like the idea of the << >> operator overloading and adding as a friend of those classes. However I am not sure how this doesn\'t break encapsulation. When can these exceptions stay within the strictness that is OOP? 回答1: Firstly (IMO) don't listen to