C++ Friend Functions: Benefits & Tradeoffs

  • Context: C/C++ 
  • Thread starter Thread starter neurocomp2003
  • Start date Start date
  • Tags Tags
    C++ Functions
Click For Summary
SUMMARY

The discussion centers on the use of C++ friend functions versus member functions, particularly for functions like Print, Save, and Load in a 3D engine and neural network context. Friend functions allow access to protected members of a class but are often viewed as a design cop-out. The memory and computational trade-offs between friend functions and member functions are negligible, with member functions incurring slight overhead due to vtables. The conversation emphasizes the importance of proper organization in code structure while questioning the necessity of getters and setters.

PREREQUISITES
  • Understanding of C++ friend functions and their purpose
  • Familiarity with object-oriented design principles
  • Knowledge of memory management in C++
  • Basic concepts of namespaces and static functions in C++
NEXT STEPS
  • Research best practices for using C++ friend functions
  • Explore object-oriented design patterns in C++
  • Learn about memory management and performance optimization in C++
  • Investigate the use of namespaces and static functions in C++ for code organization
USEFUL FOR

C++ developers, software engineers working on 3D engines or neural networks, and anyone interested in optimizing code organization and design patterns in C++.

neurocomp2003
Messages
1,359
Reaction score
4
Sup,
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)?

thx
Neurocomp
 
Technology news on Phys.org
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 {
Save(...1...)
Save(...2..)
}
OR
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 what's 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:

Similar threads

  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
9K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 89 ·
3
Replies
89
Views
6K
  • · Replies 7 ·
Replies
7
Views
12K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K