Use static in classes (when needed) in modern C++

Поделиться
HTML-код
  • Опубликовано: 13 сен 2024

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

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

    Correction: I've missed a prefix in one example. At 01:53 there should be a Foo:: prefix for the Bar() function definition in the bottom of the screen.
    Thanks for watching and keep that feedback coming! 🙏 Enjoy!

  • @BjörnHolmberg-x5b
    @BjörnHolmberg-x5b 9 месяцев назад +2

    Great series of lectures. Really valuable even if you have been working with C++ for many years.

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

      Thanks for telling this! Helps my motivation a lot! 🙏

  • @zamf
    @zamf 9 месяцев назад +2

    4:52 One example where calling a static function on a class object is when the class is templated and you already have a object instance of that class template. In this case if you call the method using the `::` syntax you're violating the DRY principle because you're repeating the class template with all of its template arguments.
    For example:
    MyClass obj = ...;
    //do something with obj
    MyClass::static_method(); //you're repeating MyClass which is prone to typos
    Instead if you use the object there's no repetition:
    MyClass obj = ...;
    //do something with obj
    obj.static_method(); //fine, it calls MyClass::static_method()
    And in the future, if you want to change the template arguments, let's say from MyClass to MyClass you only have to change it in one place.
    Of course, you can always do `using MyClassInst = MyClass;` and then use the alias instead of the full template instantiation but it requires writing more code.

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

      I see the point to a degree. But as you say, I’d use a using there probably. Also, if we get an auto variable in a lambda or smth, so that we don’t have its type spelled out, I’d still use a using and the :: to be more explicit in my intent.

  • @iKeaght
    @iKeaght 9 месяцев назад +2

    Really good video ! keep it up ^^

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

      Thanks so much! Glad you liked it! 🙏

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

    Great content. Please, record more videos!

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

    I used too much inside out side const constexpr; it’s helpful

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

      Hmm, I don’t fully understand what you want to say, could you maybe rephrase?

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

    Superb video

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

    1:53 should be void Foo::Bar() {...}

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

      Oh! You’re right! I wonder how that slipped through my automatic checks… I’ll add a correction!

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

      Ok, I've figured out why I did not catch the error with my automatic validation of all the code. The reason was that for whatever reason I used the -c flag in all my code snippet validation commands. Probably a copy-paste mistake. 🤦‍♂ Anyway, this is now fixed in the script that corresponds to the lecture and the correction is added to the video. Unfortunately this is as good as it gets without re-uploading the video. Please tell me what you think!

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

    You sure there are (static) methods in
    C++? ;)

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

      Hmmm, I'm not sure I'm getting what you are asking 😅

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

      @@CodeForYourself aren’t they just called functions? The word method is normally not used in C++ (nitpicking)…

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

      I’m not sure. When it comes to functions that belong to a class I’m used to calling them methods or functions interchangeably. But honestly I’ve never thought about it much. 🧐

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

    So funny. Just tell as why it exists and we will understand. Why would you use it over regular functions for instance.

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

      Well, that’s what I tried to do in the video 😅 Anyway, in my understanding static for classes exists just to aid encapsulation, meaning that we can bundle any static data or functions into a class if they will ever be used in relation to that class. This helps readability and “scoping” of these variables and functions. Does this make any sense?