#### Archived

This topic is now archived and is closed to further replies.

# Is the use of class friends considered bad?

This topic is 5933 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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
{
IDirect3DDevice8* m_pD3DDev;
... //application specific stuff, (initialization, main loop...)
}

{
CApplication m_pParent;
}

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

##### Share on other sites
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 */
/*=========================================*/

##### Share on other sites
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.

1. 1
2. 2
Rutin
22
3. 3
4. 4
5. 5
frob
12

• 9
• 17
• 9
• 31
• 16
• ### Forum Statistics

• Total Topics
632617
• Total Posts
3007452

×