Destructor Basics | C++ Tutorial

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

КОМЕНТАРІ • 51

  • @menachemlevi
    @menachemlevi 11 місяців тому +6

    you are a king of explanation

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

    Thanks for providing the source code, this way we can open the source code and don't need to go back and forward in the video.

  • @thee3a
    @thee3a 2 роки тому +3

    Super clear explanations. Thank you!!

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

    Hello , I am using Visual Studio 2022
    And when I type like your code 02:00
    The Destructor does not activate!!
    But it becomes like a function being called by the object , Is it because of the Compiler or a new language update ? Or is there something wrong ?

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

      Sorry to bother you, I was using this code at the bottom of the main() function.
      system("pause>0");

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

      @@snowandl4195 OK, so it sounds like you solved the issue on your own? Or is it still an issue? The code for this example is here, it should work: github.com/portfoliocourses/cplusplus-example-code/blob/main/destructor_basics.cpp

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

      @@PortfolioCourses Yes, I solved the problem, and this code is system("pause>0");
      It is the cause of the problem

  • @dengzhonghan5125
    @dengzhonghan5125 2 місяці тому

    Awsome tutorials. Thanks for your effort!

  • @ayoubmounadi2142
    @ayoubmounadi2142 Рік тому +1

    Awesome video. Thank you so much 🙏

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

      You’re welcome Ayoub, I’m glad you enjoyed it! :-)

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

    ones I run clear the g++ to compile it and now at the 3rd compilation I get this? [error: cannot convert ‘double*’ to ‘double’ in assignment
    12 | number = (double *) malloc(sizeof(double));] - did you see any issues here in my line? Thank you first run all go but during the Video I enter 3lines more and then... :-(
    Thank you for this Video I'm a beginner in C++
    I found the failure!
    private:
    double *number; //here I loose the * and then this Error occur

  • @chenchen-eo4yf
    @chenchen-eo4yf 10 місяців тому

    Hey, all great tutorial videos out there, thx! I want to ask that some videos are covered in C but not in C++, might due to the reason C++ tutorial is not done, so could I use those "missing" videos in C to cover the same concept in C++?like pointer, dynamic allocation, etc. Cause Im taking C++ course rn.

    • @PortfolioCourses
      @PortfolioCourses  10 місяців тому

      For the most part.... yes. C++ basically maintains compatibility with C for the most part. So if a program works in C, it will work in C++. But this is only "mostly" true, there are some small differences and exceptions here and there too. e.g. in C we can have:
      int *data = malloc(sizeof(int) * 50);
      but in C++ we need to have:
      int *data = (int) malloc(sizeof(int) * 50);

    • @PortfolioCourses
      @PortfolioCourses  10 місяців тому

      Also, is that the horse from Horsin' Around?

    • @chenchen-eo4yf
      @chenchen-eo4yf 10 місяців тому

      hahh I hear the voice. love the interaction. thank u@@PortfolioCourses

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

    simple and to the point explanation thank you

  • @Architector120
    @Architector120 2 роки тому +2

    I love your lessons!
    Сan you make a video on how to create a binary like "BMP" with file header.

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому +2

      Thank you. I will add this to the list of video requests. :-)

  • @Thunderking777-ml8
    @Thunderking777-ml8 6 місяців тому

    Is is good to use delete operator inside the destructor?

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

      Sometimes we really should in order to “clean up” things that the object is responsible for cleaning up. Like if you delete a Person object and a Hand object is created and “owned” by that Person object from initialization until deletion then the Person object should delete it, for example.

    • @Thunderking777-ml8
      @Thunderking777-ml8 6 місяців тому

      @@PortfolioCoursesOkay, Got it! Btw Your content is just mind blowing!🔥Keep sharing knowledge with begginers like us!

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

    Sir your tutorials are so helpful. Thank you a lot.

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

      You're very welcome Tomi, I'm glad you find them helpful! :-)

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

    Great content ! Keep on the great work !

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

    Using malloc and free inside of a C++ class in 2022 is a very edgy style choice. "That's a bold strategy cotton!"

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

      For a basic double array it won't matter, but I suppose new learners watching this video might not know that or what malloc is either... maybe one day I'll make another destructor video using new instead.

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

      @@PortfolioCourses Its not too hard to understand what it is doing, I just found it a wild contrast of the old C style and modern C++ tools.
      Though if going modern in a new video you should probably should just skip right to a unique_ptr or make_unique;
      new and delete have been superseded for over a decade [but yes they are still popular in the wild]

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

      Smart pointers are pretty neat and something I want to cover as well. I’m not a big fan of how many ways it’s possible to do things in C++, I’ve come to prefer more opinionated languages and technologies like Python over the years.

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

      ​@@PortfolioCourses The wide number of options is just a natural outcome of maintaining decades of backward compatibility. However most of those other options are essentially deprecated and only exist for that backward compatibility and occasionally some niche boilerplate use cases. That is, combining "C with Classes" features with C++20 features is technically possible but not the intended use; it would be like mixing Python 1, 2, 3, and cython into a mutant program. (If Python had backward compatibility.)
      The example here of dynamic allocation, the Core C++ guidelines that accompany the standard, consider new, and delete as completely outdated, with smart pointers being a zero overhead replacement everywhere except in the writing of the smart pointers. (They are implemented as library class templates, not core built ins.)
      Malloc and Free are not part of C++, they are C, isoC++ just happens to adopt most of IsoC for compatibility. (There are a couple C keywords that are not in a C++ compiler, and a few that emulate C but are different down deep.) So while C++ is permissive for those that need it to be, it is also quite opinionated.

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

      @@mytech6779 I prefer opinionated technologies that are not permissive. I've never heard the term opinionated applied to a technology that is also permissive, that's interesting that a technology could be both at the same time. My issue with things being "essentially" deprecated is that we know people will use them anyways, whether they should or shouldn't.

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

    Hey professor, it's look like many different ways made Destructor called. So when exactly is Destructor called? Or how many ways to make Destructor called?
    When an object goes out of the scope, when the program end, or when delete operator is called?
    I'm so confused right now.

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

      It's definitely confusing, but yes, you're correct, those are the 3 main ways that a destructor is called: when the program terminates, when the scope containing the object ends, and when the delete operator is called. There are some other ways that a destructor can be called, for example if we are using inheritance in our program, that is covered in those videos. 🙂

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

      @@PortfolioCourses thank you i got it😊

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

      @@korngsamnang You're welcome! 😀

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

      @@PortfolioCourses Hi just reading the commnets- just another question - i understand that there are 3 main ways, how do you choose when do call the delete operator? Also, from what I understand, there is an implicit destructor in the class even if we don't write it, so if thats the case, when do we write one and when do we not?

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

    I feel so hard to understand pointer concept.i didn't clearly about that since i've learned c/c++.

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому +2

      I think pointers are one of the hardest things to learn in C/C++ Korng! Maybe this video will help you out: ua-cam.com/video/2GDiXG5RfNE/v-deo.html. 🙂

    • @hodayfa000h
      @hodayfa000h 5 місяців тому

      I would say the main issue with pointers the fact they didn't work with them, for example i used to use cheat engine and that really helped me grasp pointers and assembly@@PortfolioCourses

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

    great video once again

  • @cd-stephen
    @cd-stephen Рік тому

    great vid

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

    Nice

  • @mjpthetrucker9485
    @mjpthetrucker9485 14 днів тому

    So a destructor is kind of like a garbage collector under the hood? I likey..

  • @EsraUn-hf9zt
    @EsraUn-hf9zt 5 місяців тому

    I wish there were Turkish subtitles :(