Classes part 11 - friend functions (and why you should probably avoid) | Modern Cpp Series Ep. 47
Vložit
- čas přidán 9. 06. 2024
- ►Full C++ Series Playlist: • The C++ Programming La...
►Find full courses on: courses.mshah.io/
►Join as Member to Support the channel: / @mikeshah
►Lesson Description: In this lesson I discuss the 'friend' keyword in C++ and why you should probably avoid using it because it breaks encapsulation in C++ classes and structs. The friend keyword allows (in a one-way relationship) functions to access private variables of a class. The friend keyword can also (in a one way relationship) allow classes to access all of the private member variables of another class.
►CZcams Channel: / mikeshah
►Please like and subscribe to help the channel! - Věda a technologie
omg, you are my video professor in Northeastern !!!!!
I just started using friend recently and felt proud of it, because I thought I've made some progress in my cpp learning, but now it seems that was a wrong decision. I shouldn't have used friend. 😂 Actually I even tried to use the "Client-Attorney" concept to hide part of the member functions. And it really doesn't look good, although it eventually works in my case. I wonder how to hide partial information without using friend. I thought that's a good way to encapsulate things, but the problem is it will disclose everything to the friends. Thank you for the explanation!
Cheers! Yes, friend is one of those features that has to be considered carefully. The pimpl idiom can generally be useful for hiding implementation details (czcams.com/video/3mFpXNEB_AA/video.html)
@@MikeShah Thank you, Mike! I watched this video and will try to implement pIMPL next time. :D
Very nicely explained as always. Thank You.
Thank you for the kind words!
I didn't understand the second reason why we should avoid declaring friends in our classes . It starts at @10:57. Would you please explain that second reason?
Also I think that'd be great if you could post a video on the combination of declaring friends on derived and base classes with different types of inheritance :)
Thanks again for this informative video.
Cheers! The basic issue with friends classes is that they can break encapsulation when used improperly, and I think they're an easier feature to use improperly versus just using a member function in many cases. isocpp.org/wiki/faq/friends
Hello Mike,
I was wondering why I get error on 'd.k;' !
Does it mean our friendship relationship doesn’t provide us access to the private members of the Base class? I thought friendship applies to the whole subject of a class (data members, member functions and Base)
struct Base
{
public:
int i;
protected:
int j;
private:
int k;
};
struct Derived : protected Base
{
friend int main();
public:
int l;
protected:
int m;
private:
int n;
};
int main()
{
Derived d;
d.i = 1;
d.j = 2;
d.k = 3;
d.l = 4;
d.m = 5;
d.n = 6;
return 0;
}
we can create a friend class which can be used as white box testing ?
Certainly could do that! In a perfect world, you'd catch any errors calling the public member functions, but perhaps there are some testing methodologies where you'd want to inject some errorful state into an object :)