Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #86084
| From | Manfred <noname@add.invalid> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: New feature in C++ wanted: friend limited to one function only in a class |
| Date | 2022-08-26 01:59 +0200 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <te92d7$118k$1@gioia.aioe.org> (permalink) |
| References | <te8afp$3mokm$1@dont-email.me> |
On 8/25/2022 7:11 PM, JiiPee wrote:
> I had this situation many times, that I wanted to use friend in class
> but I dont want friend be able to get
> access to ALL members, only maybe one function. I wish there was a
> feature in C++ like this:
>
> class Parent
> {
> private:
> void foo() { : friend Child // new feature
> }
>
> void foo2() {
> }
> };
>
> and now Child can get access to foo() and ONLY it:
>
> class Child
> {
> public:
> Parent* m_parent{nullptr}; // later initialized ...
> void foo3() {
> // Can access only foo()
> m_parent->foo(); // this is OK
> // cannot access any other privates
> m_parent->foo2(); // this is NOT OK
> }
> };
>
> Can anybody suggest this to Bjarne? hehe
I don't think this would be a good idea ("selective" friendship, not
contacting Bjarne..)
- friend by itself should be used parsimoniously: it goes against
encapsulation, so more often than not it is a symptom of poor design
(exceptions apply, but they are in fact exceptions)
- "selective" friendship, in the way you are suggesting, breaks
encapsulation even more, in that it introduces a new level of its
fragmentation.
Others have given solutions that do what you are trying to do.
If I had to address this issue, I'd look for a different and possibly
more consistent design, e.g. simply:
class Parent_public
{
public:
void foo();
};
class Parent : public Parent_public
{
private:
void foo2();
};
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
New feature in C++ wanted: friend limited to one function only in a class JiiPee <kerrttuPoistaTama11@gmail.com> - 2022-08-25 20:11 +0300
Re: New feature in C++ wanted: friend limited to one function only in a class "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-08-25 20:13 +0200
Re: New feature in C++ wanted: friend limited to one function only in a class JiiPee <kerrttuPoistaTama11@gmail.com> - 2022-08-25 23:13 +0300
Re: New feature in C++ wanted: friend limited to one function only in a class JiiPee <kerrttuPoistaTama11@gmail.com> - 2022-08-25 23:11 +0300
Re: New feature in C++ wanted: friend limited to one function only in a class Manfred <noname@add.invalid> - 2022-08-26 01:59 +0200
Re: New feature in C++ wanted: friend limited to one function only in a class JiiPee <kerrttuPoistaTama11@gmail.com> - 2022-08-26 07:19 +0300
Re: New feature in C++ wanted: friend limited to one function only in a class JiiPee <kerrttuPoistaTama11@gmail.com> - 2022-08-26 07:25 +0300
Re: New feature in C++ wanted: friend limited to one function only in a class Paavo Helde <eesnimi@osa.pri.ee> - 2022-08-26 12:13 +0300
Re: New feature in C++ wanted: friend limited to one function only in a class JiiPee <kerrttuPoistaTama11@gmail.com> - 2022-08-26 19:29 +0300
Re: New feature in C++ wanted: friend limited to one function only in a class Paavo Helde <eesnimi@osa.pri.ee> - 2022-08-26 20:49 +0300
Re: New feature in C++ wanted: friend limited to one function only in a class Michael S <already5chosen@yahoo.com> - 2022-08-26 03:55 -0700
csiph-web