What Are All Those Places Where Initializer List Is Must In C++?

  Рет қаралды 21,189

CppNuts

CppNuts

Күн бұрын

Пікірлер: 57
@rahul-patil
@rahul-patil 3 жыл бұрын
11:25 class MyClass{ public: MyClass(Base b); } MyClass mc(b); Here, when the copy constructor is called how does object creation / (Memory allocation) takes place *without* calling *default* constructor? Thanks for demonstrating all the points.
@sukumarrepaka9399
@sukumarrepaka9399 7 жыл бұрын
In the 5th point, Base(int _x) { this->_x = _x; } will also work. Just wanted to add this point. BTW, nice explanations.
@CppNuts
@CppNuts 6 жыл бұрын
Correct!! Thanks for adding the point.
@behindthescene4406
@behindthescene4406 4 жыл бұрын
As If I know -> this is used when refering or in case if pointer ...which case is this here ?
@bhupeshpattanaik7150
@bhupeshpattanaik7150 4 жыл бұрын
@@behindthescene4406 we are referring to _x of this class and not referring to the local variable _x .... Please correct me , if wrong
@Panthera-Uncia
@Panthera-Uncia 3 жыл бұрын
​@@bhupeshpattanaik7150 "this" keyword is used when referring to the Class member in order to distinguish it from the local variable that is in the function parameter which happens to use the same variable name. i.g. Class Number { int x; int y; public: void addXandY (int x, int y) { this - > x = x; (The int x in the class is assigned to the int x in the parameter of the function addXandY) this - > y = y; (The int y in the class is assigned to the int y in the parameter of the function addXandY) cout
@JKA-sf7ll
@JKA-sf7ll 3 жыл бұрын
Yes..
@janardhanreddy1180
@janardhanreddy1180 5 жыл бұрын
Last point is amazing hattsoff to your efforts
@CppNuts
@CppNuts 5 жыл бұрын
Thanks man..
@prashantchavan2673
@prashantchavan2673 3 жыл бұрын
Your kind of videos are good, explaining lot of things about what's happening behind the code actually.
@CppNuts
@CppNuts 3 жыл бұрын
Glad you liked it !!
@harshbhavsar8588
@harshbhavsar8588 3 ай бұрын
At 6:30, still having one confusion, if we write a = x, compiler would provide copy constructor but it throws error as default constructor not available for 'One a'. Question is, Why compiler won't provide default constructor for One?
@181Ravikiran
@181Ravikiran 6 жыл бұрын
class Base { const int _x; int& _y; public: Base(int a) : _x{a}, _y{a}{ cout
@CppNuts
@CppNuts 6 жыл бұрын
Thanks Nirankush.
@videofountain
@videofountain 3 жыл бұрын
Some people are very careful to distinguish between C++ initialization and C++ assignment. Initialization happens once. Assignment might happen more than once. Of course if you choose to you can use the terms more leniently, as though not writing software. I find that confusing. I think you may want to use the words C++ initialize and C++ assign more strictly. Initialization and Assignment are not the same action. Both can modify a value. A references and const must attain initial value with initialization ... and zero assignments. Some other fine points are omitted.
@xinli6243
@xinli6243 6 жыл бұрын
Hi, I have a question on example 1 (3:08). After you call Base(10), variable _x is actually referring to an x which is a local variable in Base constructor. At this moment, x has been recycled since it is out of scope. How can you print _x ? I thought you might get an SegV error but actually you didn't. I got confused. Can you tell me what's going on in your code?
@soulimanemammar2909
@soulimanemammar2909 5 жыл бұрын
You are right ! you can't initialize a reference member with a local variable. it's an undefined behavior
@ks-op8pe
@ks-op8pe 2 жыл бұрын
Base(int& x) :_x{x} {}
@santoshsingh-ni5de
@santoshsingh-ni5de 6 жыл бұрын
For reference data member, constructor in second point should be : Base(int &x) :_x{x} {}
@CppNuts
@CppNuts 6 жыл бұрын
Plz specify video timing, while asking question about the video so that it will be easier for me to answer otherwise i have watch it again and it will be of so much time waste.
@ks-op8pe
@ks-op8pe 2 жыл бұрын
Yes, you are correct! I mean it's Base(int &x) :_x{x} {}
@smitchandi498
@smitchandi498 3 жыл бұрын
At 6:16 . Why Default Constructor is a must. You said to get object of class One in class Two, we need a default constructor in class One (In case we don's use Initializer list) But Why is it so Even without Default constructor we can create an object 'a' of class One inside class Two. Then why can't we initialise a directly with x like: Two(One x) : a{x} {};
@abrarulhaqshaik
@abrarulhaqshaik 3 жыл бұрын
the thing what I understood is, we should not create another "One" object in "Two" as because we are already passing a "One" object in "main" function. So address values may be ambiguous. Hence for not making an object, we have to use object of "One" either by using just default constructor or if not default constructor (then use initializer list)... If I'm wrong correct me pls.👉👈
@yogeshasati6210
@yogeshasati6210 9 ай бұрын
@ccpnuts 13:41 how " base copy constructor" called 2 times ..what is mean by inplace constructor..?thanks in advance
@VikashKumar-uh4kx
@VikashKumar-uh4kx 3 жыл бұрын
Nice explanation. 👍 Return *this should be there in assignment operator code.
@vyankateshdhage8315
@vyankateshdhage8315 6 жыл бұрын
In the 4th point , we can do this also #include using namespace std; class base { int x; public: base (int a):x{a}{} }; class child:public base { int y; public: child(int i,int j):base(j) { y=i; } }; int main() { base b1(31); child c1(2,3); return 0; }
@spicytuna08
@spicytuna08 6 жыл бұрын
so complicated. nicely done.
@CppNuts
@CppNuts 6 жыл бұрын
Thanks man!!
@abrarulhaqshaik
@abrarulhaqshaik 3 жыл бұрын
For a guy who comes from Python, this is an entirely wholesome, intimidating and strong level of language
@konstantinrebrov675
@konstantinrebrov675 6 жыл бұрын
You can use C++11 in-class default member initializer as Sonu Lohani wrote. If you assign it to the return value of a function, you can set it from outside at run time too. :) class Base { public: Base() = default; // declare it as the default constructor. void print() { cout = 0 && number
@bigWordsHurt47
@bigWordsHurt47 5 жыл бұрын
hey what is the color scheme you use for your editor?
@CppNuts
@CppNuts 5 жыл бұрын
sublime monokai theme
@shibisaketh
@shibisaketh 7 жыл бұрын
one more optimization: call by reference and avoid 1 copy constructor call. MyClass(const Base& b) :_b{ b }
@CppNuts
@CppNuts 7 жыл бұрын
ShibiN B Hi.. Correct... making reference call save you from making temp obj.
@shibisaketh
@shibisaketh 7 жыл бұрын
This video tutorials are very useful. Good attempt.. waiting for more.Thanks :)
@CppNuts
@CppNuts 7 жыл бұрын
ShibiN B Hi.. Thanks dude, yes will keep on uploading. :)
@TheStrelok7
@TheStrelok7 7 жыл бұрын
Base(const Base & obj){this->x = obj._x;cout
@konstantinrebrov675
@konstantinrebrov675 6 жыл бұрын
What is the difference between _x(param) and _x{param} in the initializer list? I was previously used to the () syntax.
@konstantinrebrov675
@konstantinrebrov675 6 жыл бұрын
The author CppNuts has made a lecture explaining this concept for my request. kzbin.info/www/bejne/hl66pIhjnaeZbcU
@sonulohani
@sonulohani 7 жыл бұрын
In c++11 we can even initialize read only memory in class definition also... ex: class abc { const int x=10;};
@CppNuts
@CppNuts 7 жыл бұрын
Sonu Lohani Hi.. Yes it can, I pointed how you can set from outside at run time. :)
@bensalemmohamedabderrahman5844
@bensalemmohamedabderrahman5844 4 жыл бұрын
lets say i have an array of objects named "writer" inside private of class named "book" i want to initialize this array inside the constructor of book how can i do it?
@CppNuts
@CppNuts 4 жыл бұрын
Watch the video again and try to find your answer in this.
@bensalemmohamedabderrahman5844
@bensalemmohamedabderrahman5844 4 жыл бұрын
@@CppNuts i don't know how to initialize when its an array it doesn't work for me
@bhupeshpattanaik7150
@bhupeshpattanaik7150 4 жыл бұрын
Why you used variable name. _X and not X , is it any writting convention used for intialiser lists ?
@CppNuts
@CppNuts 4 жыл бұрын
No, it is used to differentiate between data members and normal variables in member function.
@bhupeshpattanaik7150
@bhupeshpattanaik7150 4 жыл бұрын
@@CppNuts ok .... thanks Nice channel for those who like CPP Best channel I found for CPP 😍👌👍
@anand1kum1
@anand1kum1 5 жыл бұрын
how to initialize reference data member of a class in default constructor. class base{ int& _bg; public: base():_bg(0){}; //Gives error: invalid initialization of non-const reference of type 'int&' from a temporary of type 'int'| };
@CppNuts
@CppNuts 5 жыл бұрын
You can not have default constuctor if u have reference data member. Because you have to initialize ref data member while creating object, while default ctor doesn't take anything.
@nebsar
@nebsar 6 жыл бұрын
As I know, you cannot use : class Foo { int x; public: Foo(int x) : x{x} // you cannot use x{x} here, /*instead you should use:*/ Foo(int x) : x(x) { } }; I use Netbeans and I am saying this by trying the code in Netbeans.
@CppNuts
@CppNuts 6 жыл бұрын
Hi Eagle.Eye, we can actually use it. It is used for uniform initialisation. This is the link of online compiler where you can compile and check if it is working. ideone.com/jIDuSK
@黎銘-s9n
@黎銘-s9n 4 жыл бұрын
Is C++ the second official language in India? If so, I'll consider moving to India.
@kphuang
@kphuang 4 жыл бұрын
In the second point I got a warning on MacBook with g++ -std=c++14 option: warning: binding reference member 'x_' to stack allocated parameter 'x' [-Wdangling-field] class Base1 { int &x_; public: Base1(int x): x_{x} {} void print() {cout
@paul87173
@paul87173 4 жыл бұрын
these south indian accent is so funny, stop using it. Hard to understand
Why Returning Reference Is Bad Some Time In C++?
2:59
CppNuts
Рет қаралды 11 М.
The Difference Between Plain Enum And Enum Class In C++?
10:08
Непосредственно Каха: сумка
0:53
К-Media
Рет қаралды 12 МЛН
Python Lambda Functions Explained for Network Engineers
5:44
Ferds Tech Channel
Рет қаралды 3
How Compilation Works Internally In C And C++?
12:39
CppNuts
Рет қаралды 53 М.
Initializer List In C++
7:19
CppNuts
Рет қаралды 27 М.
reinterpret_cast In C++
14:33
CppNuts
Рет қаралды 45 М.
sin(x+y)°=?
11:15
Mathematopia
Рет қаралды 51
When To Use Reference Over Pointer and Vice Versa In C++
8:40
AI Is Making You An Illiterate Programmer
27:22
ThePrimeTime
Рет қаралды 223 М.
Непосредственно Каха: сумка
0:53
К-Media
Рет қаралды 12 МЛН