Classes Part 22 - Curly brace versus parenthesis and std::initializer_list | Modern cpp Series

Sdílet
Vložit
  • čas přidán 6. 05. 2022
  • ►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 three different ways to initialize a variable (the fourth would be to perhaps not initialize the variable which is bad practice). In C++ there is a difference between using curly braces and parenthesis. Parenthesis will call the constructor and setup any arguments. Curly braces may also call the right constructor, but will also prevent narrowing (i.e. conversion of types). In addition, using curly braces to initialize an object will use a constructor with an std::initializer_list if one is defined.
    ►CZcams Channel: / mikeshah
    ►Please like and subscribe to help the channel!
  • Věda a technologie

Komentáře • 42

  • @AlmondAxis987
    @AlmondAxis987 Před 8 měsíci +4

    As a self-taught c++ programmer, this was really useful. I always wondered how libraries used curly braces for Container classes like rectangles, points, or RGBA colors. Thanks!

  • @dwolrdcojp
    @dwolrdcojp Před 2 lety +2

    Clear and easy to understand example. Thanks as always!

    • @MikeShah
      @MikeShah  Před 2 lety

      You are most welcome, thank you for the continued kind words!

  • @7denis71
    @7denis71 Před rokem +1

    Awesome! Your video lessons are awesome! :) Thank you for your doing this!

    • @MikeShah
      @MikeShah  Před rokem

      Thank you for the kind words!

  • @manuvaad
    @manuvaad Před rokem

    Thank you so much. Your lessons and talks are awesome as always!

    • @MikeShah
      @MikeShah  Před rokem

      Cheers, thank you for the kind words!

  • @dhanushs1802
    @dhanushs1802 Před rokem +2

    Very neatly explained as always. Thank You.

    • @MikeShah
      @MikeShah  Před rokem

      Cheers thank you for the kind words!

  • @joebosah2727
    @joebosah2727 Před rokem

    Can’t get enough
    Carry on regardless
    Merci beaucoup

  • @sallaklamhayyen9876
    @sallaklamhayyen9876 Před rokem +1

    great teacher + valuable content = thank you so much

    • @MikeShah
      @MikeShah  Před rokem

      Cheers! Thank you for the kind words!

  • @TheOldRoots
    @TheOldRoots Před 4 měsíci +1

    Do the curly braces only prevent narrowing, or any type conversion ? Like I understand why "int a{1.4}" would give an error since you are losing information in the conversion, but would float a{3} also give an error?

    • @MikeShah
      @MikeShah  Před 4 měsíci +1

      The braces require type to match, so would need to do a{3.0f}; for instance.

  • @hani_yt678
    @hani_yt678 Před 2 lety +1

    Thank you for the video , keep it up !

  • @dolphin2954
    @dolphin2954 Před rokem +1

    Great video. Thanks.

  • @quoctrieu2413
    @quoctrieu2413 Před 2 lety +1

    Thank you very much !!! Thanks to your video i found the solution to assigne values to the obj of array (myseft-defined datatype) in this way: array obj = {val1, val2, ...}

  • @propov1802
    @propov1802 Před rokem +1

    Hey, I got a question. At 2:27 Why do you print them by reference on primitive types? Wouldn't it be faster to print them by value?

    • @MikeShah
      @MikeShah  Před rokem +1

      For primitive types (i.e. a 4-byte int) you're probably right. :)

  • @blaisofotso3439
    @blaisofotso3439 Před 2 lety +1

    Thanks Mike for answering. Looking back at the code with your answer in mind, i have just realized that no member assignment was done in the constructor with parenthesis and if i am not wrong no elements have been saved(written) to m_data.
    How can the u object be constructed with parenthesis in this case then?

    • @MikeShah
      @MikeShah  Před 2 lety +2

      m_data will just be uninitialized. You should otherwise initialize it to some default value in the constructor if desired. In a few lessons I'll show how to do this with a delegate constructor. 😉

    • @blaisofotso3439
      @blaisofotso3439 Před 2 lety

      I am eager to know how it's done . Thank you ever so much

  • @reptilicusrex4748
    @reptilicusrex4748 Před rokem +1

    Great explanation. Thanks.

  • @meganfoxyou
    @meganfoxyou Před rokem +1

    Very helpful, thank you!

  • @rango1049
    @rango1049 Před 9 měsíci +1

    thanks Mike

  • @blaisofotso3439
    @blaisofotso3439 Před 2 lety +1

    Hallo Mike, why cant we print our data when we use parentheses? @12:39 / 15:52

    • @MikeShah
      @MikeShah  Před 2 lety

      Parenthesis use the constructor ;) So m_data does not have any elements to print out.

  • @ThislsYusuf
    @ThislsYusuf Před rokem

    I got a stack-use-after-scope on address error when using AddressSanitizer. Is this a compiler bug?
    (It runs well without fsanitize) Thanks for the instruction!

    • @MikeShah
      @MikeShah  Před rokem +1

      Hard to know without seeing the code, do you have a timestamp in the video?

    • @ThislsYusuf
      @ThislsYusuf Před rokem

      ​@@MikeShah Thanks for the reply, it s the code I have after finishing the video. Hope pasting the code here isn't too cumbersome
      #include
      #include
      struct UDT{
      UDT(std::initializer_list data) : m_data(data) {
      std::cout

    • @MikeShah
      @MikeShah  Před rokem

      @@ThislsYusuf Works fine for me on g++ and clang++, perhaps a compiler bug if you're on msvc? Try pasting in compilerexplorer.com and see if it works for some other compilers.

    • @ThislsYusuf
      @ThislsYusuf Před rokem +1

      @@MikeShah On it. Thanks!

  • @SuperCoolHandle94
    @SuperCoolHandle94 Před rokem +1

    Thanks alot