friend

Unqualified name lookup applied instead of argument-dependent name lookup

╄→尐↘猪︶ㄣ 提交于 2019-12-23 16:34:06
问题 Consider an example from the standard sec 3.4.1/3: typedef int f; namespace N { struct A { friend void f(A &); operator int(); void g(A a) { int i = f(a);// f is the typedef, not the friend // function: equivalent to int(a) } }; } f(a) is postfix expression. How does compiler determine that f(a) is not a function call? I'm wondering when we have no error like f previously declared of typedef int f ; as in the following example: #include <stdio.h> typedef int foo; //error: previous declaration

How do I make main a friend of my class? [closed]

和自甴很熟 提交于 2019-12-23 11:47:46
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 8 years ago . I'm thinking this is possible, but the compiler is complaining it cannot access the protected/private members of my class. I've tried moving stuff around

Declaring set of member functions as friend by using template

孤者浪人 提交于 2019-12-23 11:32:05
问题 Given the following code: class A; struct B { static void doIt(A* pa); }; struct C { static void doIt(A* pa); }; class A { int i = 9; // below works but requires a line per each type friend void B::doIt(A* pa); friend void C::doIt(A* pa); // the below however doesn't work // template<typename T> // friend void T::doIt(A* pa); // (gcc error: member 'void T::doIt(A*)' declared as friend before type 'T' defined) // (clang just ignores the above and the error is on accessing A::i in B and C) };

Why and how to overload operator<< for printing

喜你入骨 提交于 2019-12-23 10:41:03
问题 I have written a program for implementation of stack. And I have one display function in it. This is how I wrote display function at first: template <class t> void Mystack<t>::display() { for (int i = 0; i <= top; i++) { std::cout << input[i] << " "; } } Then I was suggested by developers to write a display function to be more generic. So I wrote display function as: template <class T> void Mystack<T>::display(std::ostream &os) const { for (int i = 0; i <= top; i++) { os << input[i] << " "; }

what is the difference between friend function and friend class?

江枫思渺然 提交于 2019-12-23 09:57:19
问题 what is the difference between friend function and friend class? and where should be use of friend keyword? 回答1: In short, one is a class and one is a function. For the function, just that one function gets access to private members. For a class, the whole class and all its functions get access to the private members of the befriended class. The friend keyword is used to grant access to private data members. At times you may need a helper class or a complimentary class to access the private

friend with class but can't access private members

喜你入骨 提交于 2019-12-23 07:45:34
问题 Friend functions should be able to access a class private members right? So what have I done wrong here? I've included my .h file with the operator<< I intent to befriend with the class. #include <iostream> using namespace std; class fun { private: int a; int b; int c; public: fun(int a, int b); void my_swap(); int a_func(); void print(); friend ostream& operator<<(ostream& out, const fun& fun); }; ostream& operator<<(ostream& out, fun& fun) { out << "a= " << fun.a << ", b= " << fun.b << std:

Where is the Friend access modifier intended to be used?

回眸只為那壹抹淺笑 提交于 2019-12-22 10:19:25
问题 The only place I've seen the Friend modifier used is in the WinForms designer, as alluded to in Why is the modifier set to Friend in Winforms? and VB.NET: what does the 'friend' modifier do?. The Friend modifier appears to be an almost arbitrarily wide access level that was created to solve some historic architectural problem in VB, I just wonder if anyone has a meaningful continued use for it? I have had some desires to expose methods only to a given namespace so as to roll the

Friend functions and static data members

主宰稳场 提交于 2019-12-22 05:36:49
问题 How is unqualified name lookup being performed for names using within the friend's function body? Let's consider the following code: #include <iostream> void foo(); class A { friend void foo(){ std::cout << a << std::endl; } static int a; }; int A::a = 10; int main(){ foo(); } DEMO The Standard states in the N4296::7.3.1.2/3 [namespace.memdef] : If a friend declaration in a non-local class first declares a class, function, class template or function template the friend is a member of the

Grant access to private constructor without friends?

梦想与她 提交于 2019-12-21 04:35:10
问题 I am working on some code, where I encountered a situation similar to this one: struct Bar; struct Foo{ friend struct Bar; private: Foo(){} void f(){} void g(){} }; struct Bar { Foo* f; Bar() { f = new Foo();} ~Bar() { delete f;} }; int main(){ Bar b; } I would prefer to have Bar not as friend of Foo , because besides Foo s constructor Bar does not need access to any of Foo s private methods (and thus should not have access). Is there a way to allow only Bar to create Foo s without making

Friend function is unable to construct a unique pointer of the class

梦想的初衷 提交于 2019-12-21 03:39:28
问题 I have a certain design strategy where the constructor of my class is private and can only be constructed by friends of the class. Inside the friend function, I am trying to create a unique_pointer of my class using std::make_unique but it doesn't compile. My VC12 compiler complains c:\program files (x86)\microsoft visual studio 12.0\vc\include\memory(1639): error C2248: 'Spam::Spam' : cannot access private member declared in class 'Spam' The relevant code which fails during compilation is as