c++ - function looses friend qualifier
- Sergey (24/24) Jan 27 2009 Hello,
- Bertel Brander (6/10) Jan 27 2009 The thing is, just because I make you my friend, does
- Sergey (6/18) Jan 28 2009 Ok,
- Bertel Brander (17/21) Jan 28 2009 You can not make a struct that is internally to a function a friend
- Bertel Brander (4/12) Jan 28 2009 I should add; other than it is not possible, it would be
- Sergey (6/22) Jan 29 2009 Well, it depends how you look at it. In my situation _s::fn is part of
Hello, Any reason why this code won't compile (_s::fn is not a friend of A though it is defined inside a friend function) ? class A { int m; friend void f(A*); }; void f(A* a) { struct _s { static void fn(A* a) { a->m += 1; } }; _s::fn(a); } void main() { A a; f(&a); }
Jan 27 2009
Sergey skrev:Hello, Any reason why this code won't compile (_s::fn is not a friend of A though it is defined inside a friend function) ?The thing is, just because I make you my friend, does not mean that your kids are my friends. I this case, the function f is a friend of the class A, but classes and structs inside the function f is not. See: http://www.parashift.com/c++-faq-lite/friends.html#faq-14.4
Jan 27 2009
Ok, Is there anyway working that around ? Like making struct _s a friend (and hoping all local _s declarations will be picked up as friends) ? Thanks, Sergey. Bertel Brander wrote:Sergey skrev:Hello, Any reason why this code won't compile (_s::fn is not a friend of A though it is defined inside a friend function) ?The thing is, just because I make you my friend, does not mean that your kids are my friends. I this case, the function f is a friend of the class A, but classes and structs inside the function f is not. See: http://www.parashift.com/c++-faq-lite/friends.html#faq-14.4
Jan 28 2009
Sergey skrev:Ok, Is there anyway working that around ? Like making struct _s a friend (and hoping all local _s declarations will be picked up as friends) ?You can not make a struct that is internally to a function a friend of the class ;-( But there are allways some work-around; in this case you could change the function to: void f(A* a) { struct _s { static void fn(int& x) { x += 1; } }; _s::fn(a->m); } But the work-around will depend on the circumstances
Jan 28 2009
Bertel Brander skrev:Sergey skrev:I should add; other than it is not possible, it would be bad design to do it. Some "global" class should not have to know or care about the internals in some global function.Ok, Is there anyway working that around ? Like making struct _s a friend (and hoping all local _s declarations will be picked up as friends) ?You can not make a struct that is internally to a function a friend of the class ;-(
Jan 28 2009
I should add; other than it is not possible, it would be bad design to do it. Some "global" class should not have to know or care about the internals in some global function.Well, it depends how you look at it. In my situation _s::fn is part of 'f' which should be executed asynchronously. That's why it would be a very handy feature if friend would propagate like in some other compilers. Thanks, Sergey. Bertel Brander wrote:Bertel Brander skrev:Sergey skrev:I should add; other than it is not possible, it would be bad design to do it. Some "global" class should not have to know or care about the internals in some global function.Ok, Is there anyway working that around ? Like making struct _s a friend (and hoping all local _s declarations will be picked up as friends) ?You can not make a struct that is internally to a function a friend of the class ;-(
Jan 29 2009