friend question



 DEVELOP > c-Plus-Plus > friend question

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Marco Spatz"
Date: 11 Sep 2006 11:09:08 AM
Object: friend question
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
.

User: "Nate Barney"

Title: Re: friend question 12 Sep 2006 03:11:28 PM
Marco Spatz wrote:


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
.
User: "mlimber"

Title: Re: friend question 12 Sep 2006 03:14:55 PM
Nate Barney wrote:

Marco Spatz wrote:


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

Also consider converting it to use the Attorney-Client idiom:
http://www.ddj.com/dept/cpp/184402053
Cheers! --M
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER