restricting the scope of friend class

M

MoCha

hello everyone,
i want to know of any trick used to restrict the access scope of a
friend class (or function). ie instead of the friend class having
access to the entire internals of the class in question, let it have
access to a few private/protected members.

this problem is cropping up several times. giving the friend class
full access does not seem right n at the same time the function(s) r
not reqd by anyone else.

thanx in advance for any suggestions or comments.
 
J

John Harrison

MoCha said:
hello everyone,
i want to know of any trick used to restrict the access scope of a
friend class (or function). ie instead of the friend class having
access to the entire internals of the class in question, let it have
access to a few private/protected members.

There is no way of doing this.
this problem is cropping up several times. giving the friend class
full access does not seem right n at the same time the function(s) r
not reqd by anyone else.

Why doesn't it seem right? You are writing both classes so you are in
control. It is what the outside world sees (i.e. the interface to your two
classes) that is important because you can't control the way your classes
are used, but you have full control on the way they are written.
thanx in advance for any suggestions or comments.

john
 
J

Jeff Schwab

MoCha said:
hello everyone,
i want to know of any trick used to restrict the access scope of a
friend class (or function). ie instead of the friend class having
access to the entire internals of the class in question, let it have
access to a few private/protected members.

You could collect those members and their accessors in separate classes,
and let them grant friendship as appropriate. Let those separate
classes be public bases of your "target" class. Your derived class need
declare any new friends.

#include <iostream>

struct Int_wrapper
{
friend std::eek:stream& operator << (
std::eek:stream&, Int_wrapper const& );

Int_wrapper( int i ): m_i( i ) { }

protected:
int value( ) const { return m_i; }

private:
int m_i;
};

struct Instance_counted_int_wrapper: Int_wrapper
{
using Int_wrapper::value;

Instance_counted_int_wrapper( int i =0 ):
Int_wrapper( i ) { ++m_count; }

~Instance_counted_int_wrapper( ) { --m_count; }

int count( ) const { return m_count; }

private:
static int m_count;
};

int Instance_counted_int_wrapper::m_count = 0;

std::eek:stream& operator << ( std::eek:stream& out, Int_wrapper const& p )
{
return out << p.value( );
}

int main( )
{
Instance_counted_int_wrapper i( 3 );

std::cout << i << '\n';
}
 
F

Frank Schmitt

hello everyone,
i want to know of any trick used to restrict the access scope of a
friend class (or function). ie instead of the friend class having
access to the entire internals of the class in question, let it have
access to a few private/protected members.

this problem is cropping up several times. giving the friend class
full access does not seem right n at the same time the function(s) r
not reqd by anyone else.

Perhaps the "private interface pattern" will help.
http://www.objectmentor.com/resources/articles/privateInterface.pdf

HTH & kind regards
frank
 

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,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top