Hi Rupesh, I am using this textbook and a piece of code has me confused and you are the only person I could think of to ask. It seems like an object is being initialized within a function inside a class. is this possible? here is the code snippet below: void addInterest(int rate, int divisor) //function within DollarAmount class { DollarAmount interest { //DollarAmount is class name (is interest an object of DollarAmount?) (amount * rate + divisor / 2) /divisor }; //is this an initialization of interest? add(interest); } the book i am using is deitel c++ how to program and this is the example class given in chapter 5.
Thank You so Much Sir!! I DO HAVE ONE DOUBT: @5:01 u said {x=a} is assignment and x{a} is Initialisation. But when we do {x=a} doesnt it also perform the same operation as initialisation?
no..when object is constructed, compilers fills x with 0/garbage....later on the assignment is done x = a. Initialization list reduces one step...It directly initializes member with value when object is constructed.
Why use a member initializer list at all? It seems like the way you initialized the member variable x to start with was very intuitive and natural. This way just seems unnecessarily complicated, so I am wondering what is the payoff of having to learn a whole new syntax when a more familiar one seems to work just fine?
Hi Joel, good that you asked, here is the link where i have explained what are the different places where you will use initializer list. ruclips.net/video/vmMKXLuDgYQ/видео.html
but what if your member variable is an aggregate, wont it use aggregate initialization then? (which does allows narrowing - as far as i understand) im very confused about this whole initializer_list and aggregate initialization thing im sorry if this is completely wrong
Is this the class constructor member initializer list? Perhaps the name of video should be changed. It was clear to hear you differentiate between initialization and assignment.
A great tutrial, clear and concise. Thanks!
During member initialisation don't use (), the compiler will give a level 2 warning. It's better to use { }
sir could you explain the use of '(int)x' which you used in the program?....I am not aware of that.
It’s called typecast.
Do we have any specific name for the " : " which is used for initializing data members?
I have to tell this "U R THE BEST". U made it simple n easy.😍👍
As you already be knowing, it is collon, and thats the syntax for writing initializer list.
Thank you so much.. :)
@CppNuts I think you are using sublime text how to setup it for ubuntu
I cannot explain it here, try Google and I am sure you will get. It`s pretty common
have you ever noticed this initializer list in LEETCODE question if you check linked list question.
Hi Rupesh, I am using this textbook and a piece of code has me confused and you are the only person I could think of to ask. It seems like an object is being initialized within a function inside a class. is this possible?
here is the code snippet below:
void addInterest(int rate, int divisor) //function within DollarAmount class
{
DollarAmount interest { //DollarAmount is class name (is interest an object of DollarAmount?)
(amount * rate + divisor / 2) /divisor
}; //is this an initialization of interest?
add(interest);
}
the book i am using is deitel c++ how to program and this is the example class given in chapter 5.
thanks for your knowledge sir, You made it very simple and easy. Thanks
Thanks dude!!!
omg awesome video! you have made everything become crystal clear thanks man!!
You are most welcome dude.. :)
Thank You so Much Sir!!
I DO HAVE ONE DOUBT: @5:01 u said {x=a} is assignment and x{a} is Initialisation. But when we do {x=a} doesnt it also perform the same operation as initialisation?
it is initialisation using assignment operator
no..when object is constructed, compilers fills x with 0/garbage....later on the assignment is done x = a.
Initialization list reduces one step...It directly initializes member with value when object is constructed.
@@aniketahir101 good comment. everything is clear now, thanks :)
@@abrarulhaqshaik great to know
Why use a member initializer list at all? It seems like the way you initialized the member variable x to start with was very intuitive and natural. This way just seems unnecessarily complicated, so I am wondering what is the payoff of having to learn a whole new syntax when a more familiar one seems to work just fine?
Hi Joel, good that you asked, here is the link where i have explained what are the different places where you will use initializer list.
ruclips.net/video/vmMKXLuDgYQ/видео.html
when i use curely braces Base(int a):x{a} it is throwing error.Using curely braces instead of paranthesis is compiler dependent or what?
Yes upgrade to new compiler.
but what if your member variable is an aggregate, wont it use aggregate initialization then? (which does allows narrowing - as far as i understand)
im very confused about this whole initializer_list and aggregate initialization thing im sorry if this is completely wrong
Where is that video that was mentioned to be in description, I found too many playlist links but no video link
Please mention what video?
@@CppNuts pls check this timestamp 6:31
ruclips.net/video/vmMKXLuDgYQ/видео.html
@@CppNuts thanks .... 😊😀
There is no link in the description where initializer list has to be used! please post it!
Thanks
Added in description.
ruclips.net/video/vmMKXLuDgYQ/видео.html
how can i initialize an array of objects when its private using the initialize list?
Is this the class constructor member initializer list? Perhaps the name of video should be changed. It was clear to hear you differentiate between initialization and assignment.
Initializer list is used to initialize data members.
informative and concise. Thx!
Thanks
Awesome video
Thank you dear sir!
Welcome man!!
Awesome bro
Thanks..
thank you
Welcome