Friend classes and encapsulation



 DEVELOP > c-Plus-Plus > Friend classes and encapsulation

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Taras_96"
Date: 01 Feb 2008 01:21:04 AM
Object: Friend classes and encapsulation
HI all,
Jesse Liberty at http://newdata.box.sk/bx/c/htm/ch15.htm#Heading24
writes
"NOTE: You will often hear novice C++ programmers complain that friend
declarations "undermine" the encapsulation so important to object-
oriented programming. This is, frankly, errant nonsense. The friend
declaration makes the declared friend part of the class interface, and
is no more an undermining of encapsulation than is public derivation."
But public derivation doesn't expose the *private* data, so doesn't it
still to some extent undermine encapsulation?
.

User: "=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?="

Title: Re: Friend classes and encapsulation 01 Feb 2008 02:45:42 PM
On 2008-02-01 08:21, Taras_96 wrote:

HI all,

Jesse Liberty at http://newdata.box.sk/bx/c/htm/ch15.htm#Heading24
writes

"NOTE: You will often hear novice C++ programmers complain that friend
declarations "undermine" the encapsulation so important to object-
oriented programming. This is, frankly, errant nonsense. The friend
declaration makes the declared friend part of the class interface, and
is no more an undermining of encapsulation than is public derivation."

But public derivation doesn't expose the *private* data, so doesn't it
still to some extent undermine encapsulation?

I do not know about that particular example but friends increase
encapsulation since they allow you to do stuff that you would not other-
wise do without making some private members public (or provide accessor-
functions).
--
Erik Wikström
.

User: "Grizlyk"

Title: Re: Friend classes and encapsulation 02 Feb 2008 01:25:10 AM
Taras_96 wrote:


But public derivation doesn't expose the *private* data,
so doesn't it still to some extent undermine encapsulation?

Probably C++ development forces were originally pointed to support
inheritance as first aim, so friend access has been added only "to
be", but desing patterns, for example, required friend access at least
as advanced as inheritance: private, protected.
There is easy proposal for C++ improvement to extend friend access as
private, protected
class A
{
int private_data;
protected:
int protected_data;
//new
friend protected class C;
//old
friend class D;
};
class B: protected A
{
public:
int& get1(){ return protected_data; }
//error
int& get2(){ return private_data; }
};
class C
{
A a;
public:
int& get1(){ return a.protected_data; }
//error new
int& get2(){ return a.private_data; }
};
class D
{
A a;
public:
int& get1(){ return a.protected_data; }
//ok old
int& get2(){ return a.private_data; }
};
Some times ago i think, that the proposal is enough, but now i see,
that there are some problems here, so the proposal can be added only
as temporary solution, but C++ does not support dialects to use any
temporary standard solutions :).
Maksim A. Polyanin
http://grizlyk1.narod.ru/cpp_new
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER