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

Selectively Public Function

I have a high level class X.
I have a lower level utility class Y.

I want only class X to have access to method y in class Y. i.e. I want
class Z to have access to most methods in Y but not method y.

Without making y private and making class X a friend of Y, is there a
pattern to achieve desired results?

Technically, class Y should have no idea of class X so declaring class
X as a friend breaks the hierarchy.

Thanks,
-zip

Sep 27 '05 #1
5 1406
zippy747 wrote:
I have a high level class X.
I have a lower level utility class Y.

I want only class X to have access to method y in class Y. i.e. I want
class Z to have access to most methods in Y but not method y.

Without making y private and making class X a friend of Y, is there a
pattern to achieve desired results?

Technically, class Y should have no idea of class X so declaring class
X as a friend breaks the hierarchy.


Something like this?

class Y {
friend class yBuddy; // whatever that is
void y();
public:
void yy();
void yyy();
};

class yBuddy { // ah, that's what it is...
static void y(Y& a_y) { a_y.y(); }
friend class X; // aha!
};

class X {
void bar(Y& a_y) {
a_y.yy();
a_y.y(); // error
yBuddy::y(a_y); // a "work-around"
}
};

class Z {
void bar(Y& a_y) {
a_y.yy();
a_y.y(); // error
yBuddy::y(a_y); // error
}
};

V
Sep 27 '05 #2
zippy747 wrote:
I have a high level class X.
I have a lower level utility class Y.

I want only class X to have access to method y in class Y. i.e. I want
class Z to have access to most methods in Y but not method y.

Without making y private and making class X a friend of Y, is there a
pattern to achieve desired results?
You want Y.y() to be inaccessible by client code. Making the method y()
private is the way to go. That *is* what private means.

Technically, class Y should have no idea of class X so declaring class
X as a friend breaks the hierarchy.


Technically, you want to make an exception for client class X. Since class Y
is where access rights are specified, Y *should* be told that class X is
different from all other clients. And that is what friend declarations do.
Now, why is there a problem? You have a clean way of achieving exactly the
effect you desire through standard means of the language that anybody
mainting the code in the future will understand. Yet you are searching for
a roundabout way of doing the very same thing. It appears to me that your
problem is purely philosophical and not technical.
Best

Kai-Uwe Bux
Sep 27 '05 #3
Are we still pals?

-zip

Sep 28 '05 #4
zippy747 wrote:
Are we still pals?


Sorry, I didn't mean to be rude. To make up for it, let me elaborate a
little bit on the suggestion of Victor Bazarov. To me it seems that the
idiom he suggested solves a slightly different problem: how do I make some
private members but not all of them available to a class. Consider:
class Y {

friend class yBuddy; // whatever that is
void y();

friend class yyBuddy; // whatever that is
void yy();

public:

void yyy();

};

class yBuddy { // ah, that's what it is...
static void y(Y& a_y) { a_y.y(); }
friend class X; // aha!
};

class yyBuddy { // ah, that's what it is...
static void yy(Y& a_y) { a_y.yy(); }
friend class Z; // aha!
};

class X {
void bar(Y& a_y) {
a_y.yyy();
yBuddy::y(a_y); // work-around
yyBuddy::yy(a_y); // error
}
};

class Z {
void bar(Y& a_y) {
a_y.yyy();
yBuddy::y(a_y); // error
yyBuddy::yy(a_y); // work-around
}
};
Now, X can call y() and Z can call yy() but X cannot call yy() and Z cannot
call y(). In case that is what you need, by all means go for it. However,
note that it becomes a little more tricky to maintain this code since now
you cannot tell which classes have access to the internals of Y just by
looking at the code of Y. The definitions of access rights gets somewhat
spread out.
Best

Kai-Uwe Bux
Sep 28 '05 #5
You weren't rude. I appreciate your responses.

In order to maintain hierarchy, I made another class at the Y hierarchy
level -- class Y'. Y' is a friend to class Y and can access its
private members.

Class X inherits from Class Y' and thus can use base class Y' to access
class Y private members. I maintain hierarchy and limit access to
class Y private members to classes that inherit from Y'.

-zip

Sep 29 '05 #6

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

Similar topics

5
by: Alexander Stippler | last post by:
Hello, I have got a list of indices stored as a stl::vector and a range given by two iterators, lets say . The values in this range are not ordered, but I have another range of values, lets say...
1
by: dejausenet | last post by:
I have firefox 1.0, running on winxp Firefox blocks popups fine, but sometimes, for selected occassions only, i would like to be able to have window popup; without enabling whole domains, as...
13
by: N. Graves | last post by:
Thanks for take time to read my question!! I'm using code that will automatically delete rows of data in a field and of course when you do this Access will prompt you that you are about to...
7
by: Saintor | last post by:
What I do now is I put a value in the tag property, and using the form_current event, I run through all controls properties until the ones with the required tag value are met. Sound OK in theory,...
11
by: Michael B Allen | last post by:
Here's fragment of C from an allocator. This allocator permits the user to specify a callback to reclaim memory if necessary. If memory cannot be found the 'if (reclaim)' block is entered and the...
4
by: tarunbajaj | last post by:
Hi all, I am inserting a key in HttpContext.Cache Some keys I am caching for multiple days. Out of these once a while I need to reset cache entry of specific key. I can do a lot to reset the...
10
by: Robert Jacobson | last post by:
Hi, I'm develing a COM add-in for Microsoft Word XP that displays a form. I'd like to have the form display using the Windows XP theme. However, neither using a manifest nor calling...
3
by: Frustrated Developer via DotNetMonster.com | last post by:
I have posted a couple times on here already and found the user community to be very helpful. I took on a project before I realized how difficult a time I'm having working with a database....
0
by: ntabb | last post by:
Hello all, I have a class instance that I'm attaching to a propertygrid. I would like to find a way to change the "Browsable" attribute during runtime or programmatically from outside the class...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.