Sir, you explained it in the way that it made me feel like I am being taught by an interviewer itself, who wants all the students out there to succeed with flying colours. 👍
i didn't get properly what happens if copy constructor is applied when overriding operator returns call by value??when operation overloading is applied t1 data is copied into t2 ,but why didnt t1 data isn't coppied when copy constructor is created?? does that gets killed before coming to the print line?as the temp variable scope is only at that line instance?please explain!
Sir here in base class int x is the private variable. Even though we declare test class is friend to the base class is it possible to access the base class private variable in test class...
Hi Ram, If Base class is saying Test class is my friend then Test class can access private, protected, public data members of Base class. But reverse is not possible. For more clarity please watch my video about friend class and function in c++. Link for Friend Function And Friend Class In C++: ua-cam.com/video/mD5f3-30HEc/v-deo.html Link for Actual Use Of Friend Function And Friend Class In C++: ua-cam.com/video/QLdeKM7UVq0/v-deo.html
(t2=t3) = t1 is equivalent with t2.operator::=(t3) = t1 If the assignment operator returns by reference, then t2.operator::=(t3) will return t2. Otherwise, it will create an object temp using copy constructor: temp = t2, and return temp. I think here is the problem: the member x of temp and x of t2 ARE ONE. After the line (t2=t3) = t1, temp is deleted, and x of temp is freed. When the program goes out of scope, t2 are deleted, and x of t2 will be freed. But x of t2 is already freed (when x of temp is freed), that is why u got the error. Do I get it right?
little correction.... temp ended being copied the address of t1, due to copy constructor called... and when temp destroyed it also destroyed x of t1.... hence we are getting error.
Now it is not returned as reference, hence c style (a=b) =c; will not work. In this statement a should have c data but in your case it would still hold b data.
@@CppNuts If it not returned as a reference means the new object is created and for that *this(i.e t2) is assigned and this is returned as object and for that t1 is assigned. is this correct?
Your analogy or logic that the execution is going till the end of the program and its failing there while trying to free the memory memory which has been already been freed is wrong.
No temp object involved either. The reason for this undefined behaviour is that the chaining only works when you return a reference from function and not the value.
Base = derived; this doesnt give compile error. How is this possible without having explicitly written assignment operator? class Base { public: }; class Derived : public Base { public: }; int main() { Base bObj; Derived dObj; bObj = dObj; // this does not give compile error... return 0; }
Hi Rupesh.. same malloc freed pointer error i was trying to replicate in my code to understand the concept and for some practical, but m not getting that error. Kindly see once below code and verify error should come and why it is not coming now ? #include using namespace std; class Base { int *x; public : Base(int a=0):x{new int(a)}{} void setX(int a){*x=a;} int getX(){return *x;} Base operator = (const Base& obj) { if(this!=&obj) *x=*obj.x; return *this; } }; int main() { Base b1(20),b2, b3; (b2=b3)=b1; cout
What a genius person you are! I was shocked to see that your copy constructor method... very good one!
Thanks for such a nice comment..
Second code example where you do (t2=t3) = t1 ; is just awesome .Really good work man .
Sir, you explained it in the way that it made me feel like I am being taught by an interviewer itself, who wants all the students out there to succeed with flying colours. 👍
It's my pleasure
Thank you for continuing going in deeper and solving out each issue that you faced. 👍
Thnks man,
Keep learning..
@@CppNuts do you have a video that tells more about difference between
return this;
And
return *this;?
So in depth... amazing
The way you are explaining is fantastic :) .Great content CppNuts
Thanks man..
learned new things alway... :)
11.15 you - I don't know where the video is going me- to your ans it is awesome !!
Very Very Helpful Videos.....Please keep posted More & More Videos.......Again Thanku
I will try my best
Superb explanations...
Thanks for your comment!!
4:48 it was really rude friend... :)
Maza aa gya Rupesh bhai..
It's really very nice explanation
All my doubts are cleared now 😊
Thank you so much 😊👍
Love you ❤ @CppNuts.
Very Nice and deep thinking video...just wanted to ask yot if you could just provide some links from where you learn these cool stuffs!
I always had questions and searched for answers
Effective c++ by scott meyers is a good reference
Start with the last edition and go ahead with the previouses
Yaa i have heard about this book.
i didn't get properly what happens if copy constructor is applied when overriding operator returns call by value??when operation overloading is applied t1 data is copied into t2 ,but why didnt t1 data isn't coppied when copy constructor is created?? does that gets killed before coming to the print line?as the temp variable scope is only at that line instance?please explain!
Please mention time in the video where you have doubt, it is very hard to find where did I do something.
Awesome explanation !!
Thanks Shashwata!!
You are amazing!
Thanks
At 12:08 we are not getting the correct output though, why?
How to write our own assignment operator for our class if our class has a const data member or reference member variable?
Sir here in base class int x is the private variable. Even though we declare test class is friend to the base class is it possible to access the base class private variable in test class...
Hi Ram,
If Base class is saying Test class is my friend then Test class can access private, protected, public data members of Base class.
But reverse is not possible.
For more clarity please watch my video about friend class and function in c++.
Link for Friend Function And Friend Class In C++: ua-cam.com/video/mD5f3-30HEc/v-deo.html
Link for Actual Use Of Friend Function And Friend Class In C++: ua-cam.com/video/QLdeKM7UVq0/v-deo.html
@@CppNuts Thanku sir now got cleared 😍😍😍
You are always welcome!!
Nice, thx a lot!
You are most welcome!!
Also if we are getting right output without copy constructor previously then why should we make one ?
(t2=t3) = t1 is equivalent with t2.operator::=(t3) = t1
If the assignment operator returns by reference, then t2.operator::=(t3) will return t2.
Otherwise, it will create an object temp using copy constructor: temp = t2, and return temp. I think here is the problem: the member x of temp and x of t2 ARE ONE. After the line (t2=t3) = t1, temp is deleted, and x of temp is freed. When the program goes out of scope, t2 are deleted, and x of t2 will be freed. But x of t2 is already freed (when x of temp is freed), that is why u got the error.
Do I get it right?
little correction.... temp ended being copied the address of t1, due to copy constructor called... and when temp destroyed it also destroyed x of t1.... hence we are getting error.
Can you please create videos on templates. Questions such as design generic queue or stack.
Great idea..
6:32 I removed "&" as you did but still compiled successfully. Really confusing
Now it is not returned as reference, hence c style (a=b) =c; will not work.
In this statement a should have c data but in your case it would still hold b data.
@@CppNuts If it not returned as a reference means the new object is created and for that *this(i.e t2) is assigned and this is returned as object and for that t1 is assigned. is this correct?
is that is the reason, print statements gave 10(t1), 0(t2),0(t2)
Your analogy or logic that the execution is going till the end of the program and its failing there while trying to free the memory memory which has been already been freed is wrong.
No temp object involved either. The reason for this undefined behaviour is that the chaining only works when you return a reference from function and not the value.
Double delete should cause program crash ..was that a compiler error or crash ?
It will be a run time error i.e crash
Base = derived; this doesnt give compile error. How is this possible without having explicitly written assignment operator?
class Base
{
public:
};
class Derived : public Base
{
public:
};
int main()
{
Base bObj;
Derived dObj;
bObj = dObj; // this does not give compile error...
return 0;
}
Compiler provides it implicitly.
Assignment operator returns a reference to the left operand.
Does copy constructor get called if you return Test by value?
Hi Rupesh.. same malloc freed pointer error i was trying to replicate in my code to understand the concept and for some practical, but m not getting that error. Kindly see once below code and verify error should come and why it is not coming now ?
#include
using namespace std;
class Base
{
int *x;
public :
Base(int a=0):x{new int(a)}{}
void setX(int a){*x=a;}
int getX(){return *x;}
Base operator = (const Base& obj)
{
if(this!=&obj)
*x=*obj.x;
return *this;
}
};
int main()
{
Base b1(20),b2, b3;
(b2=b3)=b1;
cout
Hit the like button for the amazing replica of the like button!! Very Good Video!!!
Thanks man i tried my best for like button!! 😀
😍😍😍
Thanks..
The error was , there needs to be a copy construtor