Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #86092

Re: New feature in C++ wanted: friend limited to one function only in a class

From Paavo Helde <eesnimi@osa.pri.ee>
Newsgroups comp.lang.c++
Subject Re: New feature in C++ wanted: friend limited to one function only in a class
Date 2022-08-26 12:13 +0300
Organization A noiseless patient Spider
Message-ID <tea2s8$3ufb4$1@dont-email.me> (permalink)
References <te8afp$3mokm$1@dont-email.me>

Show all headers | View raw


25.08.2022 20:11 JiiPee kirjutas:
> 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


You mean the C++ committee, not Bjarne. And I think everybody agrees C++ 
has already too many features, so it would be hard to propose a new one, 
especially related to things like friends which are kind of frown upon 
anyway.

Meanwhile, what about this? Instead of a private function accessible to 
all friends define a public function accessible only to a "privileged 
friend":

class Child1;
class Child2;

class Child1Key {
	friend class Child1;
	Child1Key() {}
};

class Child2Key {
	friend class Child2;
	Child2Key() {}
};

class Parent {
public:
	void foo(Child1Key key) {}
	void foo2(Child2Key key) {}
};

class Child1 {
public:
Parent* m_parent{ nullptr }; // later initialized ...
void foo3() {
	// Can access only foo()
	m_parent->foo(Child1Key{}); // this is OK
		 // cannot access any other privates
	m_parent->foo2(Child2Key{}); // this is NOT OK
}
};




Back to comp.lang.c++ | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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