473,499 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Permitting access to only a subset of the public methods

Hi,

I'm not sure if i'm asking the question correctly but anyway here it
is.

Say I have 3 classes - class A, class B, class R.
1) A and B are the building blocks and R is like a repository that
stores objects of A and B.
2) A is at the lowest level and should "know about" only other As. B
should know only about As and other Bs.
3) R has 3 types of public methods - (i) methods to add A,B (ii)
methods to access A, B and (iii) all other methods.

For now I pass a pointer to R to both A and B so that they can access
information from R as required. However, this exposes all public
methods (ie., (i), (ii) and (iii)) of R to both A and B.

How do I change it so that A and B can only access methods of type
(ii)? (The rationale is that A/B will never add another A/B and they
shouldn't be doing anything else related to R). Is there a way to make
a subclass of R called Rsub and pass that pointer to A,B? Is that a
good way to do it?

I have included code below.

Thanks.
#include <iostream>
#include <vector>

using namespace std;

class A;
class B;
class R;

class A {
public:
A (R* r, const int& i) : rp(r), N(i) {}
int getN (void) const { return N; }
void fA (const int& idx)
{
// Access an A object at index idx through rp
}
private:
R* rp;
int N;
};

class B {
public:
B (R* r, const string& s) : rp(r), str(s) {}
string getStr (void) const { return str; }
void fB1 (const int& idx)
{
// Access an A object at index idx through rp
}
void fB2 (const int& idx)
{
// Access a B object at index idx through rp
}
private:
R* rp;
string str;
};

class R {
public:
// Methods to add A, B
// void addA (const int& i) { A a(i); vA.push_back(a); }
// void addB (const string& s) { B b(s); vB.push_back(b); }
void addA (const A& a) { vA.push_back(a); }
void addB (const B& b) { vB.push_back(b); }

// Methods to access information for A,B
int getAN (const int& idx) const { return vA.at(idx).getN(); }
string getBstr (const int& idx) const { return
vB.at(idx).getStr(); }
// ... some more info

// All other methods
// ...
private:
vector<AvA;
vector<BvB;
};

int main() {
R rep;

A a1(&rep, 100);
A a2(&rep, 200);
rep.addA(a1);
rep.addA(a2);
cout << rep.getAN(0) << " and " << rep.getAN(1) << endl;

// a2.fA(0); // should return info about a1

B b1(&rep, "dog");
B b2(&rep, "cat");
rep.addB(b1);
rep.addB(b2);
cout << rep.getBstr(0) << " and " << rep.getBstr(1) << endl;

// b2.fB1(0); // should return info about a1
// b2.fB2(0); // should return info about b1

return 0;
}
Jul 17 '08 #1
2 1763
fg*********@gmail.com wrote in news:e790868e-086d-4cb4-bb40-
2c**********@w7g2000hsa.googlegroups.com:
Hi,

I'm not sure if i'm asking the question correctly but anyway here it
is.

Say I have 3 classes - class A, class B, class R.
1) A and B are the building blocks and R is like a repository that
stores objects of A and B.
2) A is at the lowest level and should "know about" only other As. B
should know only about As and other Bs.
3) R has 3 types of public methods - (i) methods to add A,B (ii)
methods to access A, B and (iii) all other methods.

For now I pass a pointer to R to both A and B so that they can access
information from R as required. However, this exposes all public
methods (ie., (i), (ii) and (iii)) of R to both A and B.

How do I change it so that A and B can only access methods of type
(ii)? (The rationale is that A/B will never add another A/B and they
shouldn't be doing anything else related to R). Is there a way to make
a subclass of R called Rsub and pass that pointer to A,B? Is that a
good way to do it?

I have included code below.

Thanks.
#include <iostream>
#include <vector>

using namespace std;

class A;
class B;
class R;

class A {
public:
A (R* r, const int& i) : rp(r), N(i) {}
int getN (void) const { return N; }
void fA (const int& idx)
{
// Access an A object at index idx through rp
}
private:
R* rp;
int N;
};

class B {
public:
B (R* r, const string& s) : rp(r), str(s) {}
string getStr (void) const { return str; }
void fB1 (const int& idx)
{
// Access an A object at index idx through rp
}
void fB2 (const int& idx)
{
// Access a B object at index idx through rp
}
private:
R* rp;
string str;
};

class R {
public:
// Methods to add A, B
// void addA (const int& i) { A a(i); vA.push_back(a); }
// void addB (const string& s) { B b(s); vB.push_back(b); }
void addA (const A& a) { vA.push_back(a); }
void addB (const B& b) { vB.push_back(b); }

// Methods to access information for A,B
int getAN (const int& idx) const { return vA.at(idx).getN(); }
string getBstr (const int& idx) const { return
vB.at(idx).getStr(); }
// ... some more info

// All other methods
// ...
private:
vector<AvA;
vector<BvB;
};

int main() {
R rep;

A a1(&rep, 100);
A a2(&rep, 200);
rep.addA(a1);
rep.addA(a2);
cout << rep.getAN(0) << " and " << rep.getAN(1) << endl;

// a2.fA(0); // should return info about a1

B b1(&rep, "dog");
B b2(&rep, "cat");
rep.addB(b1);
rep.addB(b2);
cout << rep.getBstr(0) << " and " << rep.getBstr(1) << endl;

// b2.fB1(0); // should return info about a1
// b2.fB2(0); // should return info about b1

return 0;
}
Well, the first thing that comes to mind is to use interfaces.

class IR_A {
public:
virtual void addA(const A& a) = 0;
virtual int getAN(const int& idx) = 0;
virtual ~IR_A(){}
};

class IR_B {
public:
virtual void addB(const B& b) = 0;
virtual string getBstr(const int& idx) = 0;
virtual ~IR_B(){}
};

class R : public IR_A, public IR_B {
public:
// Methods to add A, B
// void addA (const int& i) { A a(i); vA.push_back(a); }
// void addB (const string& s) { B b(s); vB.push_back(b); }
void addA (const A& a) { vA.push_back(a); }
void addB (const B& b) { vB.push_back(b); }

// Methods to access information for A,B
int getAN (const int& idx) const { return vA.at(idx).getN(); }
string getBstr (const int& idx) const { return
vB.at(idx).getStr(); }
// ... some more info

// All other methods
// ...
private:
vector<AvA;
vector<BvB;
};

Then, you change your A and B classes to use them...

class A {
public:
A (IR_A * r, const int& i) : rp(r), N(i) {}
int getN (void) const { return N; }
void fA (const int& idx)
{
// Access an A object at index idx through rp
}
private:
IR_A * rp;
int N;
};

class B {
public:
B (IR_B* r, const string& s) : rp(r), str(s) {}
string getStr (void) const { return str; }
void fB1 (const int& idx)
{
// Access an A object at index idx through rp
}
void fB2 (const int& idx)
{
// Access a B object at index idx through rp
}
private:
IR_B * rp;
string str;
};

Main can stay the same. If you want a bit more added security, you can
make all the methods of R which participate in IR_A or IR_B private. Then
you can only access those methods through one of the two interfaces.

Hope that is of some help.

joe
Jul 17 '08 #2
I am suspecting a fundamental misunderstanding of the underlying
problem and the simplified case presented here is quite reflecting
that. Anyway, passing a subclass of R to A/B isn't going to be able to
do anything to make certain public methods of R to unavailable to A
and B. I can think of a few solutions based on similar design
patterns, obscurity through encapsulation (containment) or
polymorphically though levels of base classes preferably abstract
(like interfaces in java).
So using your own example, contain R in RCA or RCAB and provide them
to A or B respectively or create a (fully abstract) AR and ABR,
inherit R from them, that is AR->ABR->R, construct R but pass R as AR
or ABR to A or AB respectively.
In both of the above cases, have public only those methods that A or B
should see.

- Murali.

On Jul 17, 11:19*am, Joe Greer <jgr...@doubletake.comwrote:
fgh.vbn....@gmail.com wrote in news:e790868e-086d-4cb4-bb40-
2c0fbdde2...@w7g2000hsa.googlegroups.com:


Hi,
I'm not sure if i'm asking the question correctly but anyway here it
is.
Say I have 3 classes - class A, class B, class R.
1) A and B are the building blocks and R is like a repository that
stores objects of A and B.
2) A is at the lowest level and should "know about" only other As. B
should know only about As and other Bs.
3) R has 3 types of public methods - (i) methods to add A,B (ii)
methods to access A, B and (iii) all other methods.
For now I pass a pointer to R to both A and B so that they can access
information from R as required. However, this exposes all public
methods (ie., (i), (ii) and (iii)) of R to both A and B.
How do I change it so that A and B can only access methods of type
(ii)? (The rationale is that A/B will never add another A/B and they
shouldn't be doing anything else related to R). Is there a way to make
a subclass of R called Rsub and pass that pointer to A,B? Is that a
good way to do it?
I have included code below.
Thanks.
#include <iostream>
#include <vector>
using namespace std;
class A;
class B;
class R;
class A {
public:
* * A (R* r, const int& i) : rp(r), N(i) {}
* * int getN (void) const { return N; }
* * void fA (const int& idx)
* * * * {
* * * * * * // Access an A object at index idx through rp
* * * * }
private:
* * R* rp;
* * int N;
};
class B {
public:
* * B (R* r, const string& s) : rp(r), str(s) {}
* * string getStr (void) const { return str; }
* * void fB1 (const int& idx)
* * * * {
* * * * * * // Access an A object at index idx through rp
* * * * }
* * void fB2 (const int& idx)
* * * * {
* * * * * * // Access a B object at index idx through rp
* * * * }
private:
* * R* rp;
* * string str;
};
class R {
public:
* * // Methods to add A, B
// * * void addA (const int& i) { A a(i); vA.push_back(a); }
// * * void addB (const string& s) { B b(s); vB.push_back(b); }
* * void addA (const A& a) { vA.push_back(a); }
* * void addB (const B& b) { vB.push_back(b); }
* * // Methods to access information for A,B
* * int getAN * (const int& idx) const { return vA.at(idx).getN(); }
* * string getBstr (const int& idx) const { return
vB.at(idx).getStr(); }
* * // ... some more info
* * // All other methods
* * // ...
private:
* * vector<AvA;
* * vector<BvB;
};
int main() {
* * R rep;
* * A a1(&rep, 100);
* * A a2(&rep, 200);
* * rep.addA(a1);
* * rep.addA(a2);
* * cout << rep.getAN(0) << " and " << rep.getAN(1) << endl;
* * // a2.fA(0); // should return info about a1
* * B b1(&rep, "dog");
* * B b2(&rep, "cat");
* * rep.addB(b1);
* * rep.addB(b2);
* * cout << rep.getBstr(0) << " and " << rep.getBstr(1) << endl;
* * // b2.fB1(0); // should return info about a1
* * // b2.fB2(0); // should return info about b1
* * return 0;
}

Well, the first thing that comes to mind is to use interfaces.

class IR_A {
public:
* *virtual void addA(const A& a) = 0;
* *virtual int getAN(const int& idx) = 0;
* *virtual ~IR_A(){}

};

class IR_B {
public:
* *virtual void addB(const B& b) = 0;
* *virtual string getBstr(const int& idx) = 0;
* *virtual ~IR_B(){}

};

class R : public IR_A, public IR_B {
public:
* * // Methods to add A, B
// * * void addA (const int& i) { A a(i); vA.push_back(a); }
// * * void addB (const string& s) { B b(s); vB.push_back(b); }
* * *void addA (const A& a) { vA.push_back(a); }
* * *void addB (const B& b) { vB.push_back(b); }

* * *// Methods to access information for A,B
* * *int getAN * (const int& idx) const { return vA.at(idx).getN(); }
* * *string getBstr (const int& idx) const { return
*vB.at(idx).getStr(); }
* * *// ... some more info

* * *// All other methods
* * *// ...
*private:
* * *vector<AvA;
* * *vector<BvB;
*};

Then, you change your A and B classes to use them...

*class A {
*public:
* * *A (IR_A * r, const int& i) : rp(r), N(i) {}
* * *int getN (void) const { return N; }
* * *void fA (const int& idx)
* * * * *{
* * * * * * *// Access an A object at index idx through rp
* * * * *}
*private:
* * *IR_A * rp;
* * *int N;
*};

*class B {
*public:
* * *B (IR_B* r, const string& s) : rp(r), str(s) {}
* * *string getStr (void) const { return str; }
* * *void fB1 (const int& idx)
* * * * *{
* * * * * * *// Access an A object at index idx through rp
* * * * *}
* * *void fB2 (const int& idx)
* * * * *{
* * * * * * *// Access a B object at index idx through rp
* * * * *}
*private:
* * *IR_B * rp;
* * *string str;
*};

Main can stay the same. *If you want a bit more added security, you can
make all the methods of R which participate in IR_A or IR_B private. *Then
you can only access those methods through one of the two interfaces.

Hope that is of some help.

joe- Hide quoted text -

- Show quoted text -
Jul 17 '08 #3

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

Similar topics

12
2643
by: Manolis | last post by:
Hi, I was wondering if there is any way to make two objects of the same class to be able to access each other's private data, like this: class A { public: void access( const A& a )...
5
7362
by: Colin Anderson | last post by:
I discovered, with great excitement, this article http://www.davison.uk.net/vb2notes.asp when researching methods for emailing from Access via Notes. Unfortunatly, when I run this I get a...
7
8819
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
11
6548
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
8
4663
by: Stanislav Simicek | last post by:
Hello, I've several C++ classes that must be ported to C#. Is there any construction in C# similar to C++: class A : protected B { ... }
5
2660
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/callnetfrcom.asp The Joy of Interoperability Sometimes a revolution in programming forces you to abandon all...
9
2144
by: JT | last post by:
Here is the overall structure I will be referring to: End-program ProvideWorkFlow.dll Forms and methods that properly manipulate calls to methods in AccessUtils AccessUtils (a web service)...
4
4096
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I have a user control that is designed as below. I am creating these User Controls Dynamically in another form. They are multiple types of User Controls all with a common Interface so I can...
4
1586
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
7132
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
7178
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
7223
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...
1
6899
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
7390
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...
1
4919
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...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.