473,400 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

restricting the scope of friend class

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.
Jul 22 '05 #1
3 2229

"MoCha" <mo**************@rediffmail.com> wrote in message
news:7d*************************@posting.google.co m...
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
Jul 22 '05 #2
MoCha wrote:
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::ostream& operator << (
std::ostream&, 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::ostream& operator << ( std::ostream& out, Int_wrapper const& p )
{
return out << p.value( );
}

int main( )
{
Instance_counted_int_wrapper i( 3 );

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

Jul 22 '05 #3
mo**************@rediffmail.com (MoCha) writes:
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/resource...eInterface.pdf

HTH & kind regards
frank

--
Frank Schmitt
quattro research GmbH
e-mail: schmitt NO at SPAM quattro-research !@! dot com
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Tom Plunket | last post by:
I'm having some scoping issues, relating to the fact that different compilers do different things. Who's doing the right thing here? class MyClass { public: void Method(); private:
9
by: Neelesh | last post by:
I was reading TC++PL (Chapter 11, section 11.5.1 "finding friends") where it is mentioned that What I observe is a bit different - it is fine that there is an error for Xform, but the reason...
6
by: Daniel Klein | last post by:
I have a situation where I need to prevent just anyone from instantiating a particular class. The class is instantiated via certain methods from other classes in the solution, but those are the...
7
by: moondaddy | last post by:
I want to create a public enum that can be used throughout a project. I created an enum like this in a module: Public Enum ParentType Project = 0 Stage = 1 VIP = 2 Func = 3 Equipment = 4...
1
by: timbobd | last post by:
I have encountered a situation that I don't understand. When I call a sub of Friend scope (in an object with Friend scope), I am getting an error "Public member 'subname' not found in type...
12
by: Rennie deGraaf | last post by:
I have a system that looks like this: class AbstractBase { /* ... */ }; template <class T> class Impl : public AbstractBase { /* ... */ }; // ... Impl<int> i; Impl<float> f;...
26
by: Patient Guy | last post by:
The code below shows the familiar way of restricting a function to be a method of a constructed object: function aConstructor(arg) { if (typeof(arg) == "undefined") return (null);...
5
by: Steven T. Hatton | last post by:
This note appears in the discussion of name hiding and uniqueness: §3.3 #4 This note is item #6 in the discussion of "Point of declaration" §3.3.1 #6 What exactly do these statements mean?...
4
by: Christopher | last post by:
I am surprised this hasn't come up for me more in the past, but the situation is: I need to have an interface that is usable for all I need to have an interface that is only usable for some I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.