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.
@sukumarrepaka93997 жыл бұрын
In the 5th point, Base(int _x) { this->_x = _x; } will also work. Just wanted to add this point. BTW, nice explanations.
@CppNuts6 жыл бұрын
Correct!! Thanks for adding the point.
@behindthescene44064 жыл бұрын
As If I know -> this is used when refering or in case if pointer ...which case is this here ?
@bhupeshpattanaik71504 жыл бұрын
@@behindthescene4406 we are referring to _x of this class and not referring to the local variable _x .... Please correct me , if wrong
@Panthera-Uncia3 жыл бұрын
@@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-sf7ll3 жыл бұрын
Yes..
@janardhanreddy11805 жыл бұрын
Last point is amazing hattsoff to your efforts
@CppNuts5 жыл бұрын
Thanks man..
@prashantchavan26733 жыл бұрын
Your kind of videos are good, explaining lot of things about what's happening behind the code actually.
@CppNuts3 жыл бұрын
Glad you liked it !!
@harshbhavsar85883 ай бұрын
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?
@181Ravikiran6 жыл бұрын
class Base { const int _x; int& _y; public: Base(int a) : _x{a}, _y{a}{ cout
@CppNuts6 жыл бұрын
Thanks Nirankush.
@videofountain3 жыл бұрын
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.
@xinli62436 жыл бұрын
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?
@soulimanemammar29095 жыл бұрын
You are right ! you can't initialize a reference member with a local variable. it's an undefined behavior
@ks-op8pe2 жыл бұрын
Base(int& x) :_x{x} {}
@santoshsingh-ni5de6 жыл бұрын
For reference data member, constructor in second point should be : Base(int &x) :_x{x} {}
@CppNuts6 жыл бұрын
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-op8pe2 жыл бұрын
Yes, you are correct! I mean it's Base(int &x) :_x{x} {}
@smitchandi4983 жыл бұрын
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} {};
@abrarulhaqshaik3 жыл бұрын
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.👉👈
@yogeshasati62109 ай бұрын
@ccpnuts 13:41 how " base copy constructor" called 2 times ..what is mean by inplace constructor..?thanks in advance
@VikashKumar-uh4kx3 жыл бұрын
Nice explanation. 👍 Return *this should be there in assignment operator code.
@vyankateshdhage83156 жыл бұрын
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; }
@spicytuna086 жыл бұрын
so complicated. nicely done.
@CppNuts6 жыл бұрын
Thanks man!!
@abrarulhaqshaik3 жыл бұрын
For a guy who comes from Python, this is an entirely wholesome, intimidating and strong level of language
@konstantinrebrov6756 жыл бұрын
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
@bigWordsHurt475 жыл бұрын
hey what is the color scheme you use for your editor?
@CppNuts5 жыл бұрын
sublime monokai theme
@shibisaketh7 жыл бұрын
one more optimization: call by reference and avoid 1 copy constructor call. MyClass(const Base& b) :_b{ b }
@CppNuts7 жыл бұрын
ShibiN B Hi.. Correct... making reference call save you from making temp obj.
@shibisaketh7 жыл бұрын
This video tutorials are very useful. Good attempt.. waiting for more.Thanks :)
@CppNuts7 жыл бұрын
ShibiN B Hi.. Thanks dude, yes will keep on uploading. :)
@TheStrelok77 жыл бұрын
Base(const Base & obj){this->x = obj._x;cout
@konstantinrebrov6756 жыл бұрын
What is the difference between _x(param) and _x{param} in the initializer list? I was previously used to the () syntax.
@konstantinrebrov6756 жыл бұрын
The author CppNuts has made a lecture explaining this concept for my request. kzbin.info/www/bejne/hl66pIhjnaeZbcU
@sonulohani7 жыл бұрын
In c++11 we can even initialize read only memory in class definition also... ex: class abc { const int x=10;};
@CppNuts7 жыл бұрын
Sonu Lohani Hi.. Yes it can, I pointed how you can set from outside at run time. :)
@bensalemmohamedabderrahman58444 жыл бұрын
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?
@CppNuts4 жыл бұрын
Watch the video again and try to find your answer in this.
@bensalemmohamedabderrahman58444 жыл бұрын
@@CppNuts i don't know how to initialize when its an array it doesn't work for me
@bhupeshpattanaik71504 жыл бұрын
Why you used variable name. _X and not X , is it any writting convention used for intialiser lists ?
@CppNuts4 жыл бұрын
No, it is used to differentiate between data members and normal variables in member function.
@bhupeshpattanaik71504 жыл бұрын
@@CppNuts ok .... thanks Nice channel for those who like CPP Best channel I found for CPP 😍👌👍
@anand1kum15 жыл бұрын
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'| };
@CppNuts5 жыл бұрын
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.
@nebsar6 жыл бұрын
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.
@CppNuts6 жыл бұрын
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
@黎銘-s9n4 жыл бұрын
Is C++ the second official language in India? If so, I'll consider moving to India.
@kphuang4 жыл бұрын
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
@paul871734 жыл бұрын
these south indian accent is so funny, stop using it. Hard to understand