inline variables (C++ 17 and beyond) | Modern Cpp Series Ep. 110

Поділитися
Вставка
  • Опубліковано 9 лют 2023
  • ►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 show you a useful c++ 17 feature to help you write cleaner code -- inline! This time it has to deal with how we can inline static member variables within a class (or also a namespace). Again, this is useful for a header-only library, or otherwise for making sure we only link in one copy of the variable.
    ►UA-cam Channel: / mikeshah
    ►Please like and subscribe to help the channel!
    ►Join our free community: courses.mshah.io/communities/...
  • Наука та технологія

КОМЕНТАРІ • 15

  • @qcnck2776
    @qcnck2776 Рік тому +5

    Thanks. Looking forward to more C++ vids

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

      Cheers, more coming!

  • @chillrelax5056
    @chillrelax5056 Рік тому +4

    Your videos are like masala chai! Refreshing and insightful!

  • @sanjaygatne1424
    @sanjaygatne1424 Рік тому +3

    your videos are just awesome. makes the stuff easy to understand.

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

    Thanks mike for the video..

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

    your video is amazing ,i love it

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

    Much appreciated.

  • @Byynx
    @Byynx 11 місяців тому +1

    Hi can you explain why we can declare the var "static int SomeValue" and then write "int Struct::SomeValue=63" with the same syntax as if we were creating a variable? And is it because the compiler knows "SomeValue" var has to be initialized that the linker trys to find that initialilization in each source file and that's why it executes the line "int Struct::SomeValue=63" from "Struct.cpp" file?

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

      Struct::SomeValue is part of the scope (Struct:: that is). For static variables, you typically declare in the implementation (.cpp file) the value to build the symbol.

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

    This could have been clearer, an example the main.c variable Struct s isn't apparently used and struct Struct with one member, is not concretely useful.
    Fundamentally the hpp file attempts to both declare the type and an instance of it, allocating static memory, while consts are replicatable compile time values so implicitly non-extern non global (not lvalues).
    When Struct.cpp was used static is meaninglessly contradictory as it wasn't in a function scope and it wasn't a file variable.
    How on earth is this cleaner? It just looks like hacking an erroneous data concept until it compiles.
    A hint is the compiler error message reminding that it considers Struct a class.