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