friend

What's the difference between friendship and inheritance?

我怕爱的太早我们不能终老 提交于 2019-12-18 13:38:40
问题 Suppose there are two classes A and B: class A {}; class B {}; In what aspects differ the two examples below? Example 1: class C : public A, public B {}; Example 2: class C { //private friend class A; friend class B; } 回答1: A friend can touch the private parts (pun only slightly intentional! ;) ) of whatever it is friend of, but nothing of A and B are part of C - it just means that " A and B can touch C 's private bits"). Anything "less" than private is of course also available to A and B ,

Restrict inheritance to desired number of classes at compile-time

雨燕双飞 提交于 2019-12-18 13:38:15
问题 We have a restriction that a class cannot act as a base-class for more than 7 classes. Is there a way to enforce the above rule at compile-time? I am aware of Andrew Koenig's Usable_Lock technique to prevent a class from being inherited but it would fail only when we try to instantiate the class. Can this not be done when deriving itself? The base-class is allowed to know who are its children. So i guess we can declare a combination of friend classes and encapsulate them to enforce this rule.

c++ friend function - operator overloading istream >>

随声附和 提交于 2019-12-18 04:38:12
问题 My question is in regards to friend functions as well as overloading the << and >>. From my understanding I thought friend functions could (and should) access private member variables directly. However in the case I have here the compiler would only accept my .cxx file when I used "get" functions to obtain each private variable. Here is my header file class BigNum public: // CONSTRUCTORS and DESTRUCTORS BigNum(); BigNum(int num, size_t optional_base = 10); BigNum(const char strin[], size_t

Is ADL the only way to call a friend inline function?

一笑奈何 提交于 2019-12-18 01:52:52
问题 Let us define f , as a friend function of S , inside the declaration of S : struct S { friend void f() {} }; I cannot find a way to call f . Is it true, then, that such an inline friend function can only be called with argument-dependant lookup? struct S { friend void f() {} friend void g(S const&) {} } const s; int main() { // f(); // error: 'f' was not declared in this scope // S::f(); // error: 'f' is not a member of 'S' g(s); // S::g(s); // error: 'g' is not a member of 'S' } Bonus: what

C++ friend inheritance?

走远了吗. 提交于 2019-12-18 01:27:47
问题 Does a subclass inherit, the main class' friend associations (both the main class' own and other classes friended with the main class)? Or to put it differently, how does inheritance apply to the friend keyword? To expand: And if not, is there any way to inherit friendship? I have followed Jon's suggestion to post up the design problem: C++ class design questions 回答1: Friendship is not inherited in C++. The standard says (ISO/IEC 14882:2003, section 11.4.8): Friendship is neither inherited

Template friendship

怎甘沉沦 提交于 2019-12-17 16:46:12
问题 I'm trying to access protected variables of a template class with different template parameters. A friend declaration with template parameters is giving the following error: multiple template parameter lists are not allowed My code is template<class O_, class P_> class MyClass { //multiple template parameter lists are not allowed template<class R_> friend class MyClass<R_, P_> //syntax error: template< friend template<class R_> class MyClass<R_, P_> public: template<class R_> ACopyConstructor

How do you mark a struct template as friend?

▼魔方 西西 提交于 2019-12-17 12:19:11
问题 I have code like this: template <typename T, typename U> struct MyStruct { T aType; U anotherType; }; class IWantToBeFriendsWithMyStruct { friend struct MyStruct; //what is the correct syntax here ? }; What is the correct syntax to give friendship to the template ? 回答1: class IWantToBeFriendsWithMyStruct { template <typename T, typename U> friend struct MyStruct; }; Works in VS2008, and allows MyStruct to access the class. 回答2: According to this site, the correct syntax would be class

C++ template friend operator overloading

瘦欲@ 提交于 2019-12-17 10:42:37
问题 What is wrong with my code? template<int E, int F> class Float { friend Float<E, F> operator+ (const Float<E, F> &lhs, const Float<E, F> &rhs); }; G++ just keeps warning: float.h:7: warning: friend declaration ‘Float<E, F> operator+(const Float<E, F>&, const Float<E, F>&)’ declares a non-template function float.h:7: 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

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

自闭症网瘾萝莉.ら 提交于 2019-12-17 10:17:05
问题 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

PHP equivalent of friend or internal

ⅰ亾dé卋堺 提交于 2019-12-17 09:29:11
问题 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