C++ Friend Functions: Benefits & Tradeoffs

In summary, the conversation discusses the purpose of declaring functions as friends instead of member functions of a class, particularly in relation to organizing code for a 3D engine and neural nets. It is mentioned that friend classes and functions are sometimes necessary for accessing protected members in another class, but can also be seen as a cop-out or a way to avoid redesigning. The trade-offs in terms of memory and speed are minimal. The conversation also touches on the use of namespaces or classes for organizing functions, as well as the use of setter and getter functions and the availability of STL tutorials.
  • #1
neurocomp2003
1,366
3
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
  • #2
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
 
  • #3
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:

1. What is a friend function in C++?

A friend function in C++ is a function that is declared within a class, but is not a member of that class. This function has access to the private and protected members of the class, allowing it to manipulate and use them.

2. What are the benefits of using friend functions in C++?

Friend functions can improve the encapsulation of a class by allowing non-member functions to access and manipulate private and protected members. They can also improve code organization and readability by grouping related functions together within the class.

3. Are there any tradeoffs to using friend functions in C++?

One tradeoff of using friend functions is that they can break encapsulation if not used carefully. Allowing external functions to access private members can make it more difficult to maintain and debug code. Additionally, friend functions can only be used within the scope of the class, making them less flexible in certain situations.

4. Can a friend function be called without an object of the class?

Yes, a friend function can be called without an object of the class. Since it is not a member of the class, it can be accessed using the scope resolution operator (::) and the name of the function, without needing an object instance.

5. When should I use a friend function in C++?

Friend functions are useful in situations where you need to access private or protected members of a class from an external function, such as in operator overloading or implementing a class's non-member functions. They can also be used to improve code organization and structure.

Similar threads

  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
8K
  • Programming and Computer Science
Replies
9
Views
3K
  • Programming and Computer Science
Replies
7
Views
10K
  • Programming and Computer Science
Replies
12
Views
3K
  • Programming and Computer Science
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
4K
Back
Top