Is the use of class friends considered bad?

Started by
2 comments, last by Spartacus 21 years, 10 months ago
The subject pretty much asks the question. Is the use of friend''s in a class considered bad and should it be avoided? In my case it seems like a logical solution, but should I try to avoid it and find another way around it? Example:

class CApplication
{
CMenu* m_pMenu;
IDirect3DDevice8* m_pD3DDev;
... //application specific stuff, (initialization, main loop...)
}

class CMenu
{
CApplication m_pParent;
... //menu specific stuff
}
 
The class CApplication creates an instance of the objects CMenu, and CMenu owns a pointer to the parent who created the object. CMenu needs to access member private and protected data in CApplication (the parent), and this could be accomplished by making CMenu a friend of CApplication. Would this be a good way of doing it, or should I do it using accessors and mutators? Thanks

Real programmers don''t document, if it was hard to write it should be hard to understand

Real programmers don't document, if it was hard to write it should be hard to understand

Advertisement
In general, it''s usually considered bad design for classes to be friends of each other unless there''s some overwhelming reason they should be. In your case, I don''t think using friends is really appropritate, but it''s your call. Friends are usually used if you have 2 classes that are essentially different sides to the same coin (ex) OS and hardware) or are so tighly interwound that they constantly modify each other. Again, it''s up to you.

/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/
/*=========================================// Chem0sh// Lead Software Engineer & Tech Support// http://www.eFaces.biz=========================================*/
The usual complaint about use of friend classes is that it breaks encapsulation, but that''s not a valid generalism. The situation is that it may degrade or enhance encapsulation depending on the particular design forces at play. Times when encapsulation is degraded are usually when the interface to a class is not complete. If you are considering providing special privileges to one class to see another class''s internals, is it because there is some behaviour yet to be exposed? OTOH, do you definitely want to keep certain behaviour as an implementation detail, but have a special-case class requiring greater privileges than the rest of the world?

Most design decisions bring with them a wealth of options, each with different trade-offs, and it is your job as a s/w designer to learn what the implications of each trade-off is. This leads down the path of guessing how your system must behave in the future, which is why evolutionary design techniques are so sensible - they tell you to design the system for how it must behave now, whilst enabling it to evolve to future demands as they become apparent.
Do friends violate encapsulation?

This topic is closed to new replies.

Advertisement