Friend classes and encapsulation

T

Taras_96

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?
 
E

Erik Wikström

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).
 
G

Grizlyk

Taras_96 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top