Classes Part 22 - Curly brace versus parenthesis and std::initializer_list | Modern cpp Series
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
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!
Cheers!
Clear and easy to understand example. Thanks as always!
You are most welcome, thank you for the continued kind words!
Awesome! Your video lessons are awesome! :) Thank you for your doing this!
Thank you for the kind words!
Thank you so much. Your lessons and talks are awesome as always!
Cheers, thank you for the kind words!
Very neatly explained as always. Thank You.
Cheers thank you for the kind words!
Can’t get enough
Carry on regardless
Merci beaucoup
Derien mon ami! Cheers!
great teacher + valuable content = thank you so much
Cheers! Thank you for the kind words!
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?
The braces require type to match, so would need to do a{3.0f}; for instance.
Thank you for the video , keep it up !
Absolutely!
Great video. Thanks.
Cheers!
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, ...}
You are most welcome!
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?
For primitive types (i.e. a 4-byte int) you're probably right. :)
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?
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. 😉
I am eager to know how it's done . Thank you ever so much
Great explanation. Thanks.
Cheers!
Very helpful, thank you!
Cheers!
thanks Mike
Cheers!
Hallo Mike, why cant we print our data when we use parentheses? @12:39 / 15:52
Parenthesis use the constructor ;) So m_data does not have any elements to print out.
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!
Hard to know without seeing the code, do you have a timestamp in the video?
@@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
@@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.
@@MikeShah On it. Thanks!
Thanks alot
You're most welcome!