friend

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

回眸只為那壹抹淺笑 提交于 2019-11-27 14:21:14
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 (?) I like, in decreasing preference: passkey friend idiom passkey-door friend idiom pass-door friend idiom key-door friend

a class-key must be declared when declaring a friend

人盡茶涼 提交于 2019-11-27 13:57:25
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 have either declared the class above or at least have forward declared it. It would make sense to me

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

混江龙づ霸主 提交于 2019-11-27 13:14:22
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?? 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 half when the two halves will have different numbers of instances or different lifetimes. In these cases, the

friend AND inline method, what's the point ?

痞子三分冷 提交于 2019-11-27 11:50:40
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 even need to have access to the members. What do you think? Is the "friend" useless? Johannes Schaub -

friend declaration declares a non-template function [duplicate]

折月煮酒 提交于 2019-11-27 11:46:23
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) -Wno-non-template-friend disables this warning I've tried adding <> after << in the class declaration /

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

非 Y 不嫁゛ 提交于 2019-11-27 10:51:54
Suppose I have a class F that should be friend to the classes G (in the global namespace) and C (in namespace A ). to be friend to A::C , F must be forward declared. to be friend to G , no forward declaration of F is necessary. likewise, a class A::BF can be friend to A::C without forward declaration The following code illustrates this and compiles with GCC 4.5, VC++ 10 and at least with one other compiler. class G { friend class F; int g; }; // without this forward declaration, F can't be friend to A::C class F; namespace A { class C { friend class ::F; friend class BF; int c; }; class BF {

PHP equivalent of friend or internal

风格不统一 提交于 2019-11-27 07:35:00
Is there some equivalent of "friend" or "internal" in php? If not, is there any pattern to follow to achieve this behavior? Edit: Sorry, but standard Php isn't what I'm looking for. I'm looking for something along the lines of what ringmaster did. I have classes which are doing C-style system calls on the back end and the juggling has started to become cumbersome. I have functions in object A which take in object B as a parameter and have to call a method in object B passing in itself as an argument. The end user could call the method in B and the system would fall apart. ringmaster PHP doesn

Making a template parameter a friend?

て烟熏妆下的殇ゞ 提交于 2019-11-27 07:13:10
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 this but just now I'm curious if you actually can do something like this. It is explicitly disallowed

'friend' functions and << operator overloading: What is the proper way to overload an operator for a class?

那年仲夏 提交于 2019-11-27 06:23:44
In a project I'm working on, I have a Score class, defined below in score.h . I am trying to overload it so, when a << operation is performed on it, _points + " " + _name is printed. Here's what I tried to do: ostream & Score::operator<< (ostream & os, Score right) { os << right.getPoints() << " " << right.scoreGetName(); return os; } Here are the errors returned: score.h(30) : error C2804: binary 'operator <<' has too many parameters (This error appears 4 times, actually) I managed to get it working by declaring the overload as a friend function: friend ostream & operator<< (ostream & os,

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

拜拜、爱过 提交于 2019-11-27 06:06:47
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 around idioms/pattern of varying indirectness, where I really just want a way to have single , simple