PHP equivalent of friend or internal

PHP doesn’t support any friend-like declarations. It’s possible to simulate this using the PHP5 __get and __set methods and inspecting a backtrace for only the allowed friend classes, although the code to do it is kind of clumsy. There’s some sample code and discussion on the topic on PHP’s site: class HasFriends { private $__friends … Read more

a class-key must be declared when declaring a friend

I was surprised about this (and as a result deleted a previous incorrect answer). The C++03 standard says in 11.4: An elaborated-type-specifier shall be used in a friend declaration for a class. Then to make sure there’s no misunderstanding, it footnotes that with: The class-key of the elaborated-type-specifier is required. GCC is the only compiler … Read more

Is there any way to access private fields of a struct from another package?

There is a way to read unexported members using reflect (in Go < 1.7) func read_foo(f *Foo) { v := reflect.ValueOf(*f) y := v.FieldByName(“y”) fmt.Println(y.Interface()) } However, trying to use y.Set, or otherwise set the field with reflect will result in the code panicking that you’re trying to set an unexported field outside the package. … Read more

Why does a C++ friend class need a forward declaration only in other namespaces?

C++ Standard ISO/IEC 14882:2003(E) 7.3.1.2 Namespace member definitions Paragraph 3 Every name first declared in a namespace is a member of that namespace. If a friend declaration in a non-local class first declares a class or function (this implies that the name of the class or function is unqualified) the friend class or function is … Read more

friend AND inline method, what’s the point ?

friend inline bool operator==(MonitorObjectString& lhs, MonitorObjectString& rhs) { return(lhs.fVal==rhs.fVal); } is sometimes called friend definition, because it is a friend declaration that also defines the function. It will define the function as a non-member function of the namespace surrounding the class it appears in. Actually, the inline there is redundant: It’s implicitly declared inline if … Read more