home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!dtix!darwin.sura.net!haven.umd.edu!wam.umd.edu!krc
- From: krc@wam.umd.edu (Kevin R. Coombes)
- Newsgroups: comp.lang.c++
- Subject: Re: semi-private public methods?
- Message-ID: <1993Jan12.162217.23432@wam.umd.edu>
- Date: 12 Jan 93 16:22:17 GMT
- References: <RALPH.93Jan11143011@yo_dud.bell.ca>
- Sender: usenet@wam.umd.edu (USENET News system)
- Distribution: na
- Organization: University of Maryland, College Park
- Lines: 48
- Nntp-Posting-Host: rac1.wam.umd.edu
-
- In article <RALPH.93Jan11143011@yo_dud.bell.ca> ralph@dci.pinetree.org writes:
- >I would like to have a class that has some public methods that can
- >only be used by a certain group of classes.
- >Lets call this class A, and I want B,C,D &E to be able to use the
- >public methods on A. (no problem so far)
- >However, I don't want classes X, Y, &Z to be able to use the methods,
- >nor any other similar classes added in the future.
- >I don't want B,C,D &E to access the private data of A, so I can't make
- >them friend classes. A,B,C,D, &E aren't in the same heirarchy, so I
- >can't use inheritance to have a solution to the problem.
- >What I would like to be able to do is say that the public methods of A
- >are only public to a certain group of classes, and private to all
- >others.
- >Any ideas?
- >
-
- As my newsreader asks, are you absolutely sure that you want to do this?
-
- It sounds like you should look very carefully at the design of the classes
- before trying to do this; the wrong class is hoarding some data, or your
- design is missing a class.
-
- Can you explain why the "public" methods of A aren't really public? Do they
- allow clients to disturb the integrity of the data? Do they produce objects
- with incoherent data?
-
- Anyway, supposing you really DO want to do this, can you do the following:
-
- class A {
- public:
- void really_public_method();
- protected:
- void semi_public_method();
- private:
- int dont_touch_me;
- };
-
- class SemiPublic : public A {
- friend class B;
- friend class C;
- // etc.
- };
-
- Now B and C can access the protected methods of A, since they are part of
- the interface of SemiPublic. They can't touch the private members of A,
- because SemiPublic can't touch them.
-
- Kevin Coombes <krc@math.umd.edu>
-