Assignment Operator Overloading In C++

Поділитися
Вставка
  • Опубліковано 14 гру 2024

КОМЕНТАРІ • 63

  • @onurcanisler
    @onurcanisler 4 роки тому +2

    What a genius person you are! I was shocked to see that your copy constructor method... very good one!

    • @CppNuts
      @CppNuts  4 роки тому +1

      Thanks for such a nice comment..

  • @satyajeetkumarjha1482
    @satyajeetkumarjha1482 2 роки тому

    Second code example where you do (t2=t3) = t1 ; is just awesome .Really good work man .

  • @jatinpatel6811
    @jatinpatel6811 2 роки тому +1

    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. 👍

    • @CppNuts
      @CppNuts  2 роки тому +1

      It's my pleasure

  • @kevalthakker9928
    @kevalthakker9928 5 років тому +1

    Thank you for continuing going in deeper and solving out each issue that you faced. 👍

    • @CppNuts
      @CppNuts  5 років тому +1

      Thnks man,
      Keep learning..

    • @kevalthakker9928
      @kevalthakker9928 5 років тому

      @@CppNuts do you have a video that tells more about difference between
      return this;
      And
      return *this;?

  • @ruchad31
    @ruchad31 2 роки тому

    So in depth... amazing

  • @yasirarafat9946
    @yasirarafat9946 2 роки тому

    The way you are explaining is fantastic :) .Great content CppNuts

  • @murarichaudhary5602
    @murarichaudhary5602 6 місяців тому +1

    learned new things alway... :)

  • @bittupandey6645
    @bittupandey6645 3 роки тому

    11.15 you - I don't know where the video is going me- to your ans it is awesome !!

  • @technicalcleancodercpluspl6567
    @technicalcleancodercpluspl6567 3 роки тому

    Very Very Helpful Videos.....Please keep posted More & More Videos.......Again Thanku

    • @CppNuts
      @CppNuts  3 роки тому

      I will try my best

  • @rajeshgowda6884
    @rajeshgowda6884 5 років тому +2

    Superb explanations...

    • @CppNuts
      @CppNuts  5 років тому

      Thanks for your comment!!

  • @cid007007
    @cid007007 4 роки тому +5

    4:48 it was really rude friend... :)

  • @atuljain4677
    @atuljain4677 3 роки тому

    Maza aa gya Rupesh bhai..
    It's really very nice explanation
    All my doubts are cleared now 😊
    Thank you so much 😊👍

  • @VineethThungani
    @VineethThungani Рік тому

    Love you ❤ @CppNuts.

  • @ChandraShekhar-by3cd
    @ChandraShekhar-by3cd 5 років тому +3

    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!

    • @CppNuts
      @CppNuts  5 років тому +2

      I always had questions and searched for answers

    • @hsaidinsan6345
      @hsaidinsan6345 4 роки тому +1

      Effective c++ by scott meyers is a good reference
      Start with the last edition and go ahead with the previouses

    • @CppNuts
      @CppNuts  4 роки тому

      Yaa i have heard about this book.

  • @ramakrishnam5455
    @ramakrishnam5455 3 роки тому +1

    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!

    • @CppNuts
      @CppNuts  3 роки тому

      Please mention time in the video where you have doubt, it is very hard to find where did I do something.

  • @ShashwataMondalcse
    @ShashwataMondalcse 6 років тому +1

    Awesome explanation !!

    • @CppNuts
      @CppNuts  6 років тому

      Thanks Shashwata!!

  • @Tyokok
    @Tyokok 2 роки тому

    You are amazing!

  • @haldharpatel3915
    @haldharpatel3915 3 роки тому

    At 12:08 we are not getting the correct output though, why?

  • @kirantirth5137
    @kirantirth5137 2 роки тому

    How to write our own assignment operator for our class if our class has a const data member or reference member variable?

  • @rampandi9302
    @rampandi9302 6 років тому +1

    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...

    • @CppNuts
      @CppNuts  6 років тому +1

      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

    • @rampandi9302
      @rampandi9302 6 років тому +1

      @@CppNuts Thanku sir now got cleared 😍😍😍

    • @CppNuts
      @CppNuts  6 років тому

      You are always welcome!!

  • @TheGruselmops
    @TheGruselmops 5 років тому +1

    Nice, thx a lot!

    • @CppNuts
      @CppNuts  5 років тому

      You are most welcome!!

  • @haldharpatel3915
    @haldharpatel3915 3 роки тому

    Also if we are getting right output without copy constructor previously then why should we make one ?

  • @Kwang-Nguyen
    @Kwang-Nguyen 2 роки тому +1

    (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?

    • @murarichaudhary5602
      @murarichaudhary5602 6 місяців тому

      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.

  • @MohitYadav-nu4fu
    @MohitYadav-nu4fu 3 роки тому

    Can you please create videos on templates. Questions such as design generic queue or stack.

  • @jumbo999614
    @jumbo999614 5 років тому +1

    6:32 I removed "&" as you did but still compiled successfully. Really confusing

    • @CppNuts
      @CppNuts  5 років тому +1

      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.

    • @narayanareddybattula5465
      @narayanareddybattula5465 3 роки тому

      @@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?

    • @narayanareddybattula5465
      @narayanareddybattula5465 3 роки тому

      is that is the reason, print statements gave 10(t1), 0(t2),0(t2)

  • @amazingvibes3946
    @amazingvibes3946 6 років тому +2

    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.

    • @amazingvibes3946
      @amazingvibes3946 5 років тому +1

      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.

  • @VikashKumar-uh4kx
    @VikashKumar-uh4kx 4 роки тому

    Double delete should cause program crash ..was that a compiler error or crash ?

    • @nitin4uniti
      @nitin4uniti 3 роки тому

      It will be a run time error i.e crash

  • @KNT_19
    @KNT_19 3 роки тому

    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;
    }

    • @CppNuts
      @CppNuts  3 роки тому

      Compiler provides it implicitly.

  • @konstantinrebrov675
    @konstantinrebrov675 6 років тому

    Assignment operator returns a reference to the left operand.

    • @konstantinrebrov675
      @konstantinrebrov675 6 років тому

      Does copy constructor get called if you return Test by value?

  • @cid007007
    @cid007007 4 роки тому

    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

  • @hitendrapalsinghrao3436
    @hitendrapalsinghrao3436 6 років тому +2

    Hit the like button for the amazing replica of the like button!! Very Good Video!!!

    • @CppNuts
      @CppNuts  6 років тому +1

      Thanks man i tried my best for like button!! 😀

  • @ramiskl
    @ramiskl Рік тому

    😍😍😍

  • @aniketahir101
    @aniketahir101 4 роки тому

    The error was , there needs to be a copy construtor