www.digitalmars.com         C & C++   DMDScript  

c++ - function looses friend qualifier

reply Sergey <sergeyn funcom.com> writes:
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
parent reply Bertel Brander <bertel post4.tele.dk> writes:
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
parent reply Sergey <sergeyn funcom.com> writes:
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
parent reply Bertel Brander <bertel post4.tele.dk> writes:
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
parent reply Bertel Brander <bertel post4.tele.dk> writes:
Bertel Brander skrev:
 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 ;-(
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.
Jan 28 2009
parent Sergey <sergeyn funcom.com> writes:
 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:
 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 ;-(
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.
Jan 29 2009