C++ Friend Functions

is there a purpose to declaring functions as FRIEND rather than as member functions of a class...types of functions i'm looking towards: Print Save Load.

I'm trying to organize my code(3D engine/neural nets) and i'm looking to put those above 3 functions in a separate namespace or as friends but i don't know if it is wise.

Is there a memory trade off to using FRIEND rather than just declaring the function in the class as normally would.
Is there a computational trade off?

Are FRIENDs bad in general(not as apart of OO design)?



Staff Emeritus
Science Advisor
Gold Member
Functions aren't objects. Sticking a couple of functions like print(), save(), and load() into a class is not object-oriented design. That's just procedural design stuffed into a class. But, I digress...

Friend classes and functions are sometimes necessary when a class or function needs to be able to access protected members in another class. Honestly, most of the time, declaring friends is a cop-out, or a way to avoid having to do a lot of redesign.

The memory and speed trade-offs are almost non-existent. True member functions occupy space in vtables, thus giving member function calls a slight overhead. Each class's vtable will also occupy a bit more memory, but it's really hardly relevant.

If the most time-consuming thing your code is doing is making function calls, you are one hell of a 3D and neural net programmer!

- Warren
I knew functions aren't objects I'm just wondering about the organizing over classes(which I thought was OO-selflearning sucks)
if I wanted to organize these functions in classes/namespaces
ie. collecting all the save/load/print/draw functions for objects in both my engines using their respective names as namespaces or classes of static functions -> Save::Save(NeuralNet);

eg. namespace SAVE {
class SAVE{
static Save(...1...)
static Save(...1...)

and yeah i knew "friends" are usually copouts but inorder for me to access the protected and private members in either of theses organization schemes, i would either inline or use friends.

Would you suggest doing either of these or just leaving them as member functions in the original classes?

Oh yeah whats your opinion on sets/gets are they needed? I'm sick of writing them.

and do you know of any good STL tutorial sites? I'm too lazy to write my own DS though I have them lying around.

"If the most time-consuming thing your code is doing is making function calls, you are one hell of a 3D and neural net programmer!" yo quiero taco bell...its late and my brain isn't comprehending was that a complement or a sarcastic remark =]

either way thanks for the help
Last edited:

Related Threads for: C++ Friend Functions

  • Posted
  • Posted
  • Posted

Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving