Classes Part 30 - pIMPL (pointer to implementation) - More Stable APIs| Modern Cpp Series Ep. 67

Поділитися
Вставка
  • Опубліковано 10 чер 2022
  • ►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 talk about an important idiom that allows us to hide our implementation details in our classes. The pointer to implementation (pIMPL) idiom stores private data members and member functions in a class, while also limiting the pIMPL class's scope. This can be a way to create a more stable ABI for your applications, and even save compile times. The cost may be an additional level of indirection, and managing code in a .cpp file. In general, I would urge folks who have long lived codebases to consider this idiom.
    ►UA-cam Channel: / mikeshah
    ►Please like and subscribe to help the channel!
  • Наука та технологія

КОМЕНТАРІ • 33

  • @juanxpeke
    @juanxpeke 5 місяців тому +5

    Incredible explanation, I was struggling to understand the benefits of this tecnique but your video clarified everything :D

    • @MikeShah
      @MikeShah  5 місяців тому +1

      Cheers -- I'm happy to hear that!

  • @dhanushs1802
    @dhanushs1802 Рік тому +2

    Great video. Wonderfully explained as always. Thank you.

    • @MikeShah
      @MikeShah  Рік тому

      Cheers, thank you Dhanush!

  • @Southpaw101
    @Southpaw101 Рік тому +1

    Great Explanation and thanks for covering this.

    • @MikeShah
      @MikeShah  Рік тому

      Cheers, thank you for the kind words!

  • @k0185123
    @k0185123 3 місяці тому

    Nice!! Thank you for pointing this video to me. This is helpful!

  • @akashkhunt4191
    @akashkhunt4191 2 роки тому +2

    Your vidoes are very insightful. Please continue making more videos. Thank ypu

    • @MikeShah
      @MikeShah  2 роки тому

      Thank you for the kind words Akash!

  • @abhijittaware4360
    @abhijittaware4360 2 роки тому +2

    very well made video. thanks for uploading.

    • @MikeShah
      @MikeShah  2 роки тому

      Thank you, my pleasure!

  • @sreerajc2328
    @sreerajc2328 Рік тому +1

    Excellent video. Crisp and clear. I always had a concern that my company's legacy codes are exposing so much internal details to the customers. I am going to implement this approach.

  • @QWin-ir6yq
    @QWin-ir6yq 12 днів тому +1

    Another great video. My general understanding of pointers is improving but I’m still uncomfortable with them. For instance, why did you have to use new to prevent the segmentation error?

    • @MikeShah
      @MikeShah  11 днів тому +1

      Need to make sure something is allocated before you derefrence (i.e. access) any data that is being pointed to. This is something I put together to help folks with pointers: ua-cam.com/video/2R5cjpi9Fzw/v-deo.html

  • @ashwinkumargopivalliammal914
    @ashwinkumargopivalliammal914 Рік тому +1

    Fantastic Video thanks champ!

  • @MarcosCpp
    @MarcosCpp 2 роки тому +1

    Very good, bro!

    • @MikeShah
      @MikeShah  2 роки тому +1

      Thank you for the kind words!

  • @whirr9755
    @whirr9755 2 роки тому +3

    hey mike! can you do a video on the type erasure idiom? Also where do you learn these idioms from? Thanks!

    • @MikeShah
      @MikeShah  2 роки тому +4

      Will add that to the wish list! Idioms are learned over time through books, conference talks, and UA-cam! :)

  • @damondouglas
    @damondouglas Рік тому +2

    Related to 10:32, a std::unique_ptr was chosen over a std::shared_ptr because the Person class will be the only point of access to m_impl? I'm exploring in general what are the trade-offs between shared vs unique.

    • @damondouglas
      @damondouglas Рік тому

      This reminds me I need to rewatch ua-cam.com/video/DHu0tv2qTYo/v-deo.html and the other related smart pointer videos.

    • @MikeShah
      @MikeShah  Рік тому +1

      Correct! Generally prefer unique over shared for both performance and safety -- unless you know that the data must be shared. For pImpl pattern, unique makes sense as you have suggested :)

    • @damondouglas
      @damondouglas Рік тому +1

      This should be your next jeopardy quiz shhhh 🤫 (I confess I haven't finished you jeopardy video yet and wanted to wait so you may already have this in there)

    • @MikeShah
      @MikeShah  Рік тому +2

      @@damondouglas Not a bad idea. It will probably be worthwhile to have a few more review lessons along the way too :)

  • @scullyy
    @scullyy 5 місяців тому +1

    So people don't like RAII, but are fine with pimpl?!

    • @MikeShah
      @MikeShah  5 місяців тому

      Both can be used together, pIMPL is useful for ABI stability and information hiding, and lazy initailization. RAII is one of the favorite features of C++ because it gives deterministic creation of objects, and ensures resources are closed when terminated or otherwise exceptions are thrown.

  • @user-mu2du3np7d
    @user-mu2du3np7d 10 місяців тому

    great explanation.
    thx.
    but could you clarify one thing.
    how do you actually hide your implementation from customers?
    do you encrypt your cpp file, or do anything else? thx

    • @MikeShah
      @MikeShah  10 місяців тому

      Typically since the code is in the .cpp file the binary code is 'hidden' from customers. Folks may also obfuscate code and run a command like 'strip' to further obfuscate the generated code.

  • @antonfernando8409
    @antonfernando8409 Рік тому +1

    What's the real benefit of this idiom other than just interface separation or hiding. I guess child classes of this class will have Impact.

    • @MikeShah
      @MikeShah  Рік тому

      Information hiding is the big one. The other major one would be abi stability, as a pointer will always be 8 bytes on a 64-bit system. So if you make changes to the class, those happen in the implementation, and the class size does not change.