Classes part 11 - friend functions (and why you should probably avoid) | Modern Cpp Series Ep. 47

Sdílet
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

Komentáře • 11

  • @dexter-xi1gl
    @dexter-xi1gl Před 2 lety +4

    omg, you are my video professor in Northeastern !!!!!

  • @k0185123
    @k0185123 Před 3 měsíci +1

    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!

    • @MikeShah
      @MikeShah  Před 3 měsíci +1

      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)

    • @k0185123
      @k0185123 Před 3 měsíci

      @@MikeShah Thank you, Mike! I watched this video and will try to implement pIMPL next time. :D

  • @dhanushs1802
    @dhanushs1802 Před rokem +3

    Very nicely explained as always. Thank You.

    • @MikeShah
      @MikeShah  Před rokem

      Thank you for the kind words!

  • @thestarinthesky_
    @thestarinthesky_ Před rokem +1

    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.

    • @MikeShah
      @MikeShah  Před rokem +1

      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

  • @thestarinthesky_
    @thestarinthesky_ Před rokem

    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;
    }

  • @sallaklamhayyen9876
    @sallaklamhayyen9876 Před rokem +1

    we can create a friend class which can be used as white box testing ?

    • @MikeShah
      @MikeShah  Před rokem

      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 :)