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

Поділитися
Вставка
  • Опубліковано 31 січ 2025

КОМЕНТАРІ • 14

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

    Thank you so much, this tutorial really really help me

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

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

  • @chenchen-eo4yf
    @chenchen-eo4yf 11 місяців тому +1

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

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

      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.

  • @flaviaflash2471
    @flaviaflash2471 8 місяців тому

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

    • @AnaMaddison
      @AnaMaddison 2 місяці тому

      Because total was declared to be a class variable. When that is so, in this demonstration, the scope is bound to the class, and not an instance.

  • @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

  • @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: ua-cam.com/video/2GDiXG5RfNE/v-deo.html
      new and delete: ua-cam.com/video/wopESdEVJs4/v-deo.html

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

    Thank you!