Const correctness in C++

Поделиться
HTML-код
  • Опубликовано: 28 авг 2024
  • Here we look at const correctness as one of the cornerstone parts of C++. We already know how to write our functions and classes and have access to all use cases where we can and want to use const keyword. So we cover most of these use cases here.
    📚 As always, the script to the video lives here: github.com/cpp...

Комментарии • 29

  • @CodeForYourself
    @CodeForYourself  11 месяцев назад +10

    It was kinda fun to do the sketch and I hope you guys like it 😉 Cheers! Thanks for watching!

  • @D1eg0_
    @D1eg0_ 3 месяца назад +3

    VERY USEFULL!!! thanks for making learning fun!

    • @CodeForYourself
      @CodeForYourself  3 месяца назад +1

      Glad you like it! 🙏

    • @D1eg0_
      @D1eg0_ 3 месяца назад +2

      @@CodeForYourself im exploring your vids and they are really cool 😎💪

    • @CodeForYourself
      @CodeForYourself  3 месяца назад

      @D1eg0_ really glad to hear that! Don’t forget to tell your friends! 😬

  • @shubhanshurajput7091
    @shubhanshurajput7091 11 месяцев назад +1

    Best analogy ever....

  • @thanostitan.infinity
    @thanostitan.infinity 11 месяцев назад +1

    Thank you for another wonderful video!

  • @artemmentiy7107
    @artemmentiy7107 10 месяцев назад +1

    Love your videos!

  • @gusromul3356
    @gusromul3356 9 месяцев назад +1

    nice discussion

  • @AyushSingh-zw2nw
    @AyushSingh-zw2nw 9 месяцев назад +1

    Ah, finally found your channel. A year ago I used your superawesome Modern C++ for Robotics playlist for getting the basics of modern c++. Thankyou very much for those great lecturea.
    Do you have any videos planned for multi threading in c++ and event listener and publishers?

    • @CodeForYourself
      @CodeForYourself  9 месяцев назад

      Glad that you like these videos! As for multithreading, probably towards the end, there is a long way to go until then. Also, what do you mean by "event listener and publisher"? Do you have something specific in mind?

    • @AyushSingh-zw2nw
      @AyushSingh-zw2nw 9 месяцев назад

      @@CodeForYourself event listener and publisher like how ROS publisher and subscriber works? There are event handlers in java also. So how can we write our own publisher listener mechanism in c++

    • @CodeForYourself
      @CodeForYourself  9 месяцев назад +1

      @@AyushSingh-zw2nw gotcha. I would not say it belongs to the “course of C++” I had in mind, but after that the possibilities are endless 😉

  • @omniahesham6206
    @omniahesham6206 7 месяцев назад +1

    amazing!

  • @bsdooby
    @bsdooby 9 месяцев назад +1

    The class should have its static data be const. Why? Maybe I want some class-wide bookkeeping functionality in my code…

    • @CodeForYourself
      @CodeForYourself  9 месяцев назад +1

      I agree with you to a degree. While we _can_ use static data for some bookkeeping it is, at least in my experience, quite rare. Most of the static data in a class refers to type-wide constants so that's why you see this recommendation here.

    • @bsdooby
      @bsdooby 9 месяцев назад +1

      @@CodeForYourself With this statement I completely agree ;)

  • @emperor8716
    @emperor8716 7 месяцев назад

    6:01

    • @CodeForYourself
      @CodeForYourself  7 месяцев назад

      Well, this message is a bit cryptic 😆 Is there anything I can answer here?

  • @onemoremagistr
    @onemoremagistr 11 месяцев назад +1

    Слава Україні!

  • @01MeuCanal
    @01MeuCanal 9 месяцев назад +1

    At 7:05 - "The class is doomed to live and die into a single scope". Could you clarify this situation?

    • @CodeForYourself
      @CodeForYourself  9 месяцев назад +1

      Thanks for your question! It took me a while to answer it properly. Your question forced me to look into something I believed is true for such a long time that I didn't bother to check again.
      For whatever reason I got this perception that copying an object that has constant data is impossible, which is *not* true of course. As the copy constructor usually takes a const reference, nothing forbids us to create a copy of whichever object that has a copy constructor, regardless if the donor object is const or not. So clearly my statement is not fully correct. I'll think what to do next about it being in this video. Thanks for catching it!
      That being said, I still think that largely the advice is not too bad. If we have constant non-trivial data within our object, we probably will have issues implementing a move constructor for such a class. The reason is that in order to move (aka, "steal") the data, we must modify the donor object. Which should be impossible should that object be const. This whole thing will still work for trivial types for which moving is equivalent to copying though.
      So, the particular object that has the const data is still doomed to live and die within it's scope as it cannot be moved out of it, does this make sense? That being said, a copy of such an object can be created.
      In case my explanation does not deliver my point, I prepared a small example that should illustrate the issue: godbolt.org/z/Wx8ohGj35
      Please tell me what you think!

    • @CodeForYourself
      @CodeForYourself  9 месяцев назад +1

      I've cut the video a bit to avoid stating that copying such objects is impossible. I still left the phrase about "living and dying within the same scope" due to inability to being moved. The new video will be live after the processing is finished (should take a couple of hours from now). So, please have a look tomorrow. And thanks a lot for pointing this out!

    • @01MeuCanal
      @01MeuCanal 9 месяцев назад +1

      @@CodeForYourself Many thanks for the explanation. C++ is kinda complex and very ease to forget something or confuse something sometimes.
      Thanks for the very good tutorials too.

    • @CodeForYourself
      @CodeForYourself  9 месяцев назад +1

      @@01MeuCanal thanks for understanding and your kind words! Please keep these questions coming! 🙏