473,386 Members | 1,828 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,386 software developers and data experts.

Partial accessibility to a class's interface

Hi all,

is there a way to make certain functions of a
class be accessible only by specific class?
Like "friend class whatever", but only for a few
functions?

This could be usefull in a following relationship:

Client
/ \
/ \
use/ \use
/ \
Managed Object<---->Object Manager
Lets say I have a class Widget.
I also have a manager for that class, called
WidgetManager. Widget is accessed by both a client and
a widget manager. Client shouldn't be able to access functions
used only by a WidgetManager. But I don't want to make
WidgetManager a friend of Widget because I want to hide
implementation details of the Widget from WidgetManager.

Is there a trick to do that?

Thanks,
Andy.
Jul 22 '05 #1
4 1373
Andy Venikov wrote:
is there a way to make certain functions of a
class be accessible only by specific class?
Extract those functions in a separate class, derive from it,
and let the "specific class" be a friend of the base class.
Like "friend class whatever", but only for a few
functions?

This could be usefull in a following relationship:

Client
/ \
/ \
use/ \use
/ \
Managed Object<---->Object Manager
Lets say I have a class Widget.
I also have a manager for that class, called
WidgetManager. Widget is accessed by both a client and
a widget manager. Client shouldn't be able to access functions
used only by a WidgetManager. But I don't want to make
WidgetManager a friend of Widget because I want to hide
implementation details of the Widget from WidgetManager.

Is there a trick to do that?


Using the proposed scheme, you should get

class ManagedWidget {
friend class WidgetManager;
void do_manage(); // whatever that means...
};

class MyWidget : public ManagedWidget {
void do_some_other_stuff(); // hidden implementation details
public:
void do_client_stuff();
};

class WidgetManager {
...
void manage_a_widget(ManagedWidget &mw) {
mw.do_manage();
}
};

Victor
Jul 22 '05 #2
<snip>
Extract those functions in a separate class, derive from it,
and let the "specific class" be a friend of the base class.
<snip> class ManagedWidget {
friend class WidgetManager;
void do_manage(); // whatever that means...
};

class MyWidget : public ManagedWidget {
void do_some_other_stuff(); // hidden implementation details
public:
void do_client_stuff();
};

class WidgetManager {
...
void manage_a_widget(ManagedWidget &mw) {
mw.do_manage();
}
};

Victor


I see. Thanks.

But there's a problem with this solution. ManagedWidget exposes
everything to the WidgetManager
and if do_manage wants to operate with some private data (which would
always be the case) then that
private data would be accessible by WidgetManager as well.
Unless, unless we made do_manage() a virtual function and defined all
private data in MyWidget.
But that forces us to do a virtual call where it wasn't necessary
before.
Plus, what if I want to restrict client's interface only to a client?
Client part of the Widget's interface
may make sense only to clients, not to managers.

I guess in that case we could use "my firend's friend is not my
friend" thing and have a common base
structure with everything a widget needs to know.
Like this:

struct WidgetBase
{
private:
//define all widget's gadgets

friend class ManagedWidget;
friend class MyWidget;
friend class AccessedWidget
};

class ManagedWidget : virtual WidgetBase
{
friend class WidgetManager

//Specify interface for WidgetManager
void do_manage()
{
//Now we can access all gadgets, which are private in this context
}
};

class AccessedWidget : virtual WidgetBase
{
friend class Client;

//Specify client's interface
void do_client_stuff()
{
//we still can access all the gadgets from here, because we're
friends with WidgetBase
}
};

class MyWidget : ManagedWidget, AccessedWidget
{
public:
void do_something_visible_to_everyone()
{
//we still can access all the gadgets from here, because we're
friends with WidgetBase
}
};
That seems to work, or am I missing something?

Thanks,
Andy.
Jul 22 '05 #3
Andy Venikov wrote:


I see. Thanks.

But there's a problem with this solution. ManagedWidget exposes
everything to the WidgetManager
and if do_manage wants to operate with some private data (which would
always be the case) then that
private data would be accessible by WidgetManager as well.
Unless, unless we made do_manage() a virtual function and defined all
private data in MyWidget.
But that forces us to do a virtual call where it wasn't necessary
before.


That's the price you have to pay.
On the other hand the class names suggest that you are dealing
with some GUI elements. Does it really matter that much to spend
0.0000001 seconds in a virtual function call, when the user will
never notice this delay?

[snip code which has a lot of friend definitions]

I wouldn't do it that way.
That's too much trouble in the long run just to eliminate
a virtual function call.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #4
sw**********@netscape.net (Andy Venikov) wrote in message news:<15**************************@posting.google. com>...
Hi all,

is there a way to make certain functions of a
class be accessible only by specific class?
Like "friend class whatever", but only for a few
functions?
No. To me, the fact that you want to indicates that the class in
question probably isn't very well designed -- specifically it sounds
like it has a number of basically unrelated responsibilities.

[ ... ]
Lets say I have a class Widget.
I also have a manager for that class, called
WidgetManager. Widget is accessed by both a client and
a widget manager. Client shouldn't be able to access functions
used only by a WidgetManager. But I don't want to make
WidgetManager a friend of Widget because I want to hide
implementation details of the Widget from WidgetManager.


One possibility would be to use multiple inheritance:

class UI_object {
};

class managed_object {
};

class widget : public managed_object, public UI_object {
// actual implementation of a manageable widget.
};

With this scheme, the manager works with managed objects, and the
client works with UI objects. A widget is both, but code that works
with one of the base classes only sees the part that it needs to.

With this scheme, it's fairly common (though not necessary) for the
base classes to be composed primarily (or exclusively) of pure virtual
function declarations -- I.e. defining only an interface, not any
implementation.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #5

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

Similar topics

4
by: Steven Quail | last post by:
Hi to all, I am trying to get the hang of using interfaces and for some reason I am getting the "inconsistent accessibility where property ... is less accessible than property ...(cs0052)". ...
1
by: Hans De Schrijver | last post by:
I'm pretty new to C# development, so here's a newby question regarding accessibility: Scenario: class1 contains some basic properies and methods class2 inherits from class1 and adds some...
8
by: Pete Davis | last post by:
First of all, I apologize for cross-posting to so many groups, but clearly there are only 2 people on the planet that understand MS Accessibility in ..NET and I'm hoping I might reach just one of...
4
by: waltborders | last post by:
Hi, Because the blind are unable to use a mouse, keyboard navigation is key. A major difficulty is that not all windows forms controls are keyboard 'tab-able' or 'arrow-able' or have "tab...
2
by: Billy | last post by:
In .Net 2, when I have created a strongly typed dataset of a SQL table and then 'viewed' the code from the RHM menu. I am taken to the new partial class; as expected, but why do I have a partial...
2
by: PeterKellner | last post by:
I'm trying to understand how asp.net 2.0 ties together the codebehind. I've made a simple page, turned debug on so the *0.cs,*1.cs,*2.cs files don't get erased. My first point of confusion is...
7
by: Adrian Hawryluk | last post by:
Can one define a partial class (first part containing constants like enums) and then define the rest of the class elsewhere? I ask this because I've had in the past needed to defined two classes...
2
by: Joseph Geretz | last post by:
When I create a Form, the VB IDE creates the following files in the following hierarchy: Form1.cs Form1.Designer.cs Form1.resx Both Form1.cs and Form1.Designer.cs are partial implementations...
6
by: satyanarayan sahoo | last post by:
What’s the role of Partial keyword which is bydefault written for a class in 2.0 ? public partial class Form1 : Form
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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,...
0
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...

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.