473,785 Members | 2,299 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2250

"MoCha" <mo************ **@rediffmail.c om> wrote in message
news:7d******** *************** **@posting.goog le.com...
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_counte d_int_wrapper: Int_wrapper
{
using Int_wrapper::va lue;

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

~Instance_count ed_int_wrapper( ) { --m_count; }

int count( ) const { return m_count; }

private:
static int m_count;
};

int Instance_counte d_int_wrapper:: m_count = 0;

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

int main( )
{
Instance_counte d_int_wrapper i( 3 );

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

Jul 22 '05 #3
mo************* *@rediffmail.co m (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
1779
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
2533
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 is not that Xform is not in scope - the reason is that the class's size is not known. Also, the second line is compiling fine - without any problems. In other words, I find that the declaration of a friend function or a class inside a class _does_...
6
2642
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 only classes/methods allowed to do this. How can I prevent all other classes from creating a new instance of my 'restricted' class? Or, to put it another way, how can the restricted class 'know' who is trying to instantiate it? Thanks,
7
1450
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 Idea = 5
1
2235
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 'myType'". Any idea why? Since Friend scope should be visible within my assembly, and everying is taking place within a single project, I don't understand why this error is occuring. Here is some illustrative code, where I would get the error in the...
12
1789
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; std::vector<AbstractBase*> vec; vec.push_back(&i); vec.push_back(&f);
26
2152
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); this.property1 = arg; this.property2 = aConstantDefinedGlobally; this.method1 = function (anArg) {
5
1826
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? "laborated-type-specifiers and friend declarations may introduce a
4
1613
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 do not really know of a good way to achieve this. If I use friend functions, I can no longer make methods virtual, right? Example:
0
9489
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10162
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10100
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7509
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5396
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.