friendship and pImpl idiom

J

jimmy

Hi,

I had a class Foo that grants friendship to a class FooFriend. I then
wanted to use the pImpl idiom to FooFriend. So I have something like
this,

class FooFriend
{
private:
class FooFriendImpl;
FooFriendImpl *pImpl;
}

class FooFriend;
class FooFriend::FooFriendImpl;
class Foo
{
friend class FooFriend;
friend class FooFriend::FooFriendImpl; // error here
}

I would like to keep class FooFriend::FooFriendImpl but not sure if
this is possible. Any suggestions? I would think many people have come
accross this problem.

-Jimmy
 
A

Alf P. Steinbach

* jimmy:
Hi,

I had a class Foo that grants friendship to a class FooFriend. I then
wanted to use the pImpl idiom to FooFriend. So I have something like
this,

class FooFriend
{
private:
class FooFriendImpl;
FooFriendImpl *pImpl;
}

Missing semicolon. Hence: is this the actual code? Very little can be
said about errors in the actual code based on hypothetical example.

class FooFriend;
class FooFriend::FooFriendImpl;
class Foo
{
friend class FooFriend;
friend class FooFriend::FooFriendImpl; // error here

You're accessing a private (inaccessible) feature of FooFriend.

Missing semicolon.

I would like to keep class FooFriend::FooFriendImpl but not sure if
this is possible. Any suggestions? I would think many people have come
accross this problem.

class Foo;

class X
{
friend class Foo;
private:
class Y;
};

class Foo
{
friend class X;
friend class X::Y;
};
 
J

jimmy

But then Foo has access to X's private stuff. That's worse. I'll just
stick to what I have. -b
 
A

Alf P. Steinbach

* jimmy:
But then Foo has access to X's private stuff. That's worse. I'll just
stick to what I have. -b

Please quote what you're replying to.

That said, just make the declaration of Y public instead of private; I
presumed there was some reason you wanted it private, and that implied
friendship granted to Foo.

The whole point of PIMPL is that nobody has access to the innards of Y,
except the implementation file, regardless of whether the class name is
accessible elsewhere.
 

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,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top