friend question

M

Marco Spatz

Hi,

we moved our build platform to the "next" generation ;) (VS 2005 and gcc
4.0) and our code has build (with some minor changes) so far. But now I
met a strange problem with the friend keyword (I know I should be
careful using this ;) ).

I made a reproducer for it:

class ClassA
{
public:
ClassA(void);
~ClassA(void);

protected:
void doA();
void doB();
};

class ClassB
{
friend void ClassA::doA();

public:
ClassB(void);
~ClassB(void);

protected:
void doWonderfulThings();
};

Both compiler (and the comeau online compiler) complain about the
'friend void ClassA::doA();' line for the same reason. They say, that
they cannot access the protected member doA() of ClassA. This hasn't
been a problem with the old compilers. And I know workarounds for this.
But I think this is strange as ClassB isn't accessing doA() but does
only allow it to access all it's member functions (public, protected,
private). I took a look at the standard but haven't found an answer. So
I'm curious. Is this code above legal?

Thanks in advance,

Ciao,
Marco
 
N

Nate Barney

Marco said:
class ClassA
{
public:
ClassA(void);
~ClassA(void);

protected:
void doA();
void doB();
};

class ClassB
{
friend void ClassA::doA();

public:
ClassB(void);
~ClassB(void);

protected:
void doWonderfulThings();
};

Both compiler (and the comeau online compiler) complain about the
'friend void ClassA::doA();' line for the same reason. They say, that
they cannot access the protected member doA() of ClassA.

Seems like a correct message to me.
This hasn't been a problem with the old compilers.

You're right. I tried the above code in g++ 3.2.3 and it compiled
without
problems. It's probably just a bug which was corrected in the newer
compilers.
But I think this is strange as ClassB isn't accessing doA() but does only
allow it to access all it's member functions (public, protected,private).

Not sure exactly what you mean here. In the above code, ClassA::doA()
isn't called, but it needs to be accessible for one to refer to it.
Otherwise,
the encapsulation would be broken. Since ClassA::doA() is protected,
ClassB can't see it, so it can't declare it as a friend.
I took a look at the standard but haven't found an answer. So I'm curious.
Is this code above legal?

I don't have a copy of the standard handy, but I don't believe the
above
code is legal.

Nate
 
M

mlimber

Nate said:
Seems like a correct message to me.


You're right. I tried the above code in g++ 3.2.3 and it compiled
without
problems. It's probably just a bug which was corrected in the newer
compilers.


Not sure exactly what you mean here. In the above code, ClassA::doA()
isn't called, but it needs to be accessible for one to refer to it.
Otherwise,
the encapsulation would be broken. Since ClassA::doA() is protected,
ClassB can't see it, so it can't declare it as a friend.


I don't have a copy of the standard handy, but I don't believe the
above
code is legal.

Nate

Also consider converting it to use the Attorney-Client idiom:

http://www.ddj.com/dept/cpp/184402053

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top