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).getS tr(); }
// ... 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;
} 2 1782 fg*********@gma il.com wrote in news:e790868e-086d-4cb4-bb40- 2c**********@w7 g2000hsa.google groups.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).getS tr(); }
// ... 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).getS tr(); }
// ... 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
I am suspecting a fundamental misunderstandin g 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...@doublet ake.comwrote:
fgh.vbn....@gma il.com wrote in news:e790868e-086d-4cb4-bb40-
2c0fbdde2...@w7 g2000hsa.google groups.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).getS tr(); }
* * // ... 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).get Str(); }
* * *// ... 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 -
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 ) {cout<<"a.value="<<a.value<<endl; }
private:
int value;
|
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
Run-time error. When I run it on an XP machine it crashes, but on an
NT box it just generates an unknown error, handled by the error
handler. I have debugged and stepped through the code and have
narrowed the issue to the point at which the...
|
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 want my users to be able to select a report, click on a command
button on a form, which will then automatically create the report as a
pdf file and save it to the user's machine.
I am using Adobe Acrobat (5.0 I think) and have Adobe Distiller as a
|
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 where the job is
running, the job runs sucessfully, PDF files got generated, everything
is good. If I scheduled the job to run at the time that I am not
logged into the server, Access is not able to print to the printer.
The error is pretty...
|
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
{
...
}
| |
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 that's
come before. To take an extreme example, suppose you have been writing
Visual Basic applications for years now. If you're like many
developers, you will have built up a substantial inventory of code in
that time. And if you've been following...
|
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)
Hide.dll
methods and data I want to remain hidden
I have a DLL, Hide.dll, that contains methods that I want to handle for
|
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 use a
Factory to create them all in a generic way.
The problem is that if I create a IMyInterface - I cannot access the
derived UserControl Methods.
I need to access the Methods derived from UserControl and also the Interface
|
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:
|
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,...
|
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...
| |
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,...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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();...
|
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...
|
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 we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |