friend

In Go, is there any way to access private fields of a struct from another package?

做~自己de王妃 提交于 2019-11-26 19:58:45
问题 I have a struct in one package that has private fields: package foo type Foo struct { x int y *Foo } And another package (for example, a white-box testing package) needs access to them: package bar import "../foo" func change_foo(f *Foo) { f.y = nil } Is there a way to declare bar to be a sort of "friend" package or any other way to be able to access foo.Foo 's private members from bar , but still keep them private for all other packages (perhaps something in unsafe )? 回答1: There is a way to

Are inner classes in C++ automatically friends?

孤人 提交于 2019-11-26 18:48:45
If I define an inner class in C++, is it automatically a friend of the class that contains it? For example, is this legal: class Outer { public: class Inner { public: void mutateOuter(Outer& o); }; private: int value; }; void Outer::Inner::mutateOuter(Outer& o) { o.value ++; // Legal? Or not? } I ask because on some compilers I've tried (VS2003) this code won't work, but I've heard at least anecdotally that it does work on some compilers. I can't find a relevant section in the C++ spec about this, and if anyone can cite something specific that would say that it is or is not legal that would be

a class-key must be declared when declaring a friend

北慕城南 提交于 2019-11-26 18:20:47
问题 The g++ compiler complains with this error when I declare a friend thusly: friend MyClass; instead of friend class MyClass; Why should the class keyword be required? (the Borland C++ compiler, BTW, does not require it.) Couldn't the compiler simply look-up MyClass in the symbol table and tell it was declared as a class? (it is obviously doing the look-up anyway because it complains when MyClass it not declared) It is not like it is making a forward declaration of the class: I still have to

Why does C++ not allow inherited friendship?

筅森魡賤 提交于 2019-11-26 17:22:06
Why is friendship not at least optionally inheritable in C++? I understand transitivity and reflexivity being forbidden for obvious reasons (I say this only to head off simple FAQ quote answers), but the lack of something along the lines of virtual friend class Foo; puzzles me. Does anyone know the historical background behind this decision? Was friendship really just a limited hack that has since found its way into a few obscure respectable uses? Edit for clarification: I'm talking about the following scenario, not where children of A are exposed to either B or to both B and its children. I

How to name this key-oriented access-protection pattern?

感情迁移 提交于 2019-11-26 16:34:18
问题 Apparently this key-oriented access-protection pattern: class SomeKey { friend class Foo; SomeKey() {} // possibly non-copyable too }; class Bar { public: void protectedMethod(SomeKey); // only friends of SomeKey have access }; ... doesn't have a known name yet, thus i'd like to find a good one for it so we can refer to it without breaking our tongues. Suggestions? It should be: succinct convey the intent of access-protection ideally imply that no proxying is required (?) 回答1: I like, in

How does the friend keyword (Class/Function) break encapsulation in C++?

感情迁移 提交于 2019-11-26 16:16:37
问题 Some programmer said that, "a friend function break the encapsulation in C++". and some programmer also said, "Friend functions do not break encapsulation; instead they naturally extend the encapsulation barrier" what does it mean?.. If a friend function breaks the encapsulation in C++ then how?? 回答1: Quote from C++ FAQ which I think describes the situation with friend and encapsulation very well. No! If they're used properly, they enhance encapsulation. You often need to split a class in

friend AND inline method, what's the point ?

不想你离开。 提交于 2019-11-26 15:47:44
问题 I see in a header that I didn't write myself the following: class MonitorObjectString: public MonitorObject { // some other declarations friend inline bool operator==(MonitorObjectString& lhs, MonitorObjectString& rhs) { return(lhs.fVal==rhs.fVal); } I can't understand why this method is declared as friend. I thought it would make sense if the function is defined in another place and needs to access the internal member of the class, but this is not the case here as it is inline and doesn't

friend declaration declares a non-template function [duplicate]

ⅰ亾dé卋堺 提交于 2019-11-26 15:43:32
问题 This question already has an answer here: overloading friend operator<< for template class 5 answers I have a base Class akin to the code below. I'm attempting to overload << to use with cout. However, g++ is saying: base.h:24: warning: friend declaration ‘std::ostream& operator<<(std::ostream&, Base<T>*)’ declares a non-template function base.h:24: warning: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here)

Making a template parameter a friend?

纵饮孤独 提交于 2019-11-26 13:05:02
问题 Example: template<class T> class Base { public: Base(); friend class T; }; Now this doesn\'t work... Is there a way of doing this? I\'m actually trying to make a general class sealer like this: class ClassSealer { private: friend class Sealed; ClassSealer() {} }; class Sealed : private virtual ClassSealer { // ... }; class FailsToDerive : public Sealed { // Cannot be instantiated }; I found this example on this site somewhere but I can\'t find it... (here) I know there are other ways of doing

clean C++ granular friend equivalent? (Answer: Attorney-Client Idiom)

折月煮酒 提交于 2019-11-26 12:52:11
问题 Why does C++ have public members that anyone can call and friend declarations that expose all private members to given foreign classes or methods but offer no syntax to expose particular members to given callers? I want to express interfaces with some routines to be invoked only by known callers without having to give those callers complete access to all privates, which feels like a reasonable thing to want. The best I could come up with myself (below) and suggestions by others so far revolve