Class Variables (i.e. "Static Member Variables") | C++ Tutorial

Поделиться
HTML-код
  • Опубликовано: 19 окт 2024
  • How to use class variables (also known as static member variables) in C++. Source code: github.com/por.... Check out www.portfolioc... to build a portfolio that will impress employers!

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

  • @flaviaflash2471
    @flaviaflash2471 5 месяцев назад

    Great ... But could you explain why your using scope resolution (::) when accessing the total

  • @tong5364
    @tong5364 Год назад

    Thank you so much, this tutorial really really help me

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      You’re very welcome, I’m glad that it helped you out! :-)

  • @chenchen-eo4yf
    @chenchen-eo4yf 7 месяцев назад +1

    Great video! but can you explain little bit why static variable can only be initialized outside of class?

    • @UnleashedCode
      @UnleashedCode 8 дней назад

      I too would like to know.... I did notice that you can initialize it using the 'inline' keyword: inline static int counter { 0 }; This compiles.... but I have seen inconsistencies with the counter results like this. Sometimes the counter starts negatively. No idea why.

  • @didiTchu
    @didiTchu 2 года назад +1

    great as always, but you didnt explain the 'star' and the 'new' that you used

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +2

      Thanks! :-) These videos may help with those concepts:
      Introduction to Pointers: ruclips.net/video/2GDiXG5RfNE/видео.html
      new and delete: ruclips.net/video/wopESdEVJs4/видео.html

  • @PEJOTAvix
    @PEJOTAvix 2 года назад

    Great tutorial. Thank you! But I'm actually having troble with another thing. I'm trying to modularize my code into MVC.
    How to I acess a controller variable from my view cpp?
    The problem is that if I declare a controller class static variable outside my class declaration (as a global variable in my controller.cpp), that variable is not accessible from my view.cpp.
    How do I fix this? Do I have to pass variables or pointers as parametrs of class methods?

    • @PortfolioCourses
      @PortfolioCourses  2 года назад

      Thanks Paulo! :-) That's a great question. I don't have a tutorial on this topic yet, but for the problem you're experiencing I would suggest looking up the topic "multifile projects in C++" as some of the tutorials out there around this topic may address what you're experiencing and/or trying to ultimately do. If you split the project up across .h and .cpp files that include one another in the correct way, what you're trying to do should be possible, but it's more than I think I can explain well in a comment. One day I will make a tutorial on this topic too, but I'm not sure when.

    • @PEJOTAvix
      @PEJOTAvix 2 года назад

      Thanks! I will.
      I found a way to access the variables by declaring them in the controller.h as you suggested on this tutorial, initializing them in the controller.cpp and importing the controller.cpp in my view.cpp.
      But I don't know if importing a cpp file into another is a good practice, tho. I'll test it today and get back to tell you if everything works fine without any linker problems
      Thanks again and shoutout from Brazil!

    • @PortfolioCourses
      @PortfolioCourses  2 года назад

      Sounds good! :-D

  • @pedllz
    @pedllz 2 года назад

    Thank you!