Thread overview
function looses friend qualifier
Jan 27, 2009
Sergey
Jan 27, 2009
Bertel Brander
Jan 28, 2009
Sergey
Jan 28, 2009
Bertel Brander
Jan 28, 2009
Bertel Brander
Jan 29, 2009
Sergey
January 27, 2009
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);
}
January 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
January 28, 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
January 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
January 28, 2009
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.
January 29, 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:
>>> 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.