Re: Friend class/function
| From: | Johannes Schlüter | Date: | Mon, 02 Nov 2015 16:47:50 +0000 |
| Subject: | Re: Friend class/function | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-89044@lists.php.net to get a copy of this message | ||
On Sun, 2015-11-01 at 17:04 +0100, georges wrote:
> I recently discovered the "friendship concept" in c++ and i really
> love the concept.
> And i wonder if the php internals would be interested to implements it
> in the next 7.x major version.
Could you provide use cases where this provides a better interface than
other alternatives? I think there are only very few cases ...
In C++ the primary use-case are binary operators which have to be
written as a free function but need access to private elements. i.e.
class Complex {
int _real, _im;
public:
Complex(int r, int i) : _real(r), _im(i) {}
friend Complex operator+(int a, const Complex& b);
};
Complex operator+(int a, const Complex& b) {
return Complex(a + b._real, b._im);
}
Here the operator+(int, const Complex&) can't be part of a class as the
first operand is a built-in while logically it belongs to the class's
interface.
(note: this specific example isn't good as we could provide accessors to
the real and imaginary part of our complex number but there are cases
where this isn't possible/wanted)
We don't have this situation in PHP, though.
johannes
Attachment: [application/pgp-signature] This is a digitally signed message part signature.asc
Attachment: [application/pgp-signature] This is a digitally signed message part signature.asc