How to write better classes in C++

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ต.ค. 2024

ความคิดเห็น • 22

  • @JohnZakaria
    @JohnZakaria 2 ปีที่แล้ว +7

    That's very good advice, I wished someone would have told me that when I started out with C++ .

  • @stephenhowe4107
    @stephenhowe4107 ปีที่แล้ว +2

    10:20 No!
    The standard mentions that if you do
    Base *p = new Derived.
    :
    delete p;
    then it is undefined behaviour, if Base does not have a virtual destructor. That is it.
    But if you never do this with your classes => you need not have a virtual Base destructor.
    An interesting example is std::unary_function and std::binary_function. They can be inherited from.
    And what do they do? They contain no data. All they do is typedef some lines based on arguments. That is it, nothing else.
    This is so certain algorithms work smoothly with them.

    • @ZenSepiol
      @ZenSepiol  ปีที่แล้ว +2

      You are right about the standard.
      I still think it is best practice to declare a virtual destructor for

  • @stephenhowe4107
    @stephenhowe4107 ปีที่แล้ว +2

    3:26: No!
    The rule of 3 is : destructor, copy constructor & copy assignment operator. It is not the default constructor.
    The rule of 5 is : same as rule of 3 + move constructor & move assignment operator
    If you need to "undo" something in the destructor, free a resource, you probably need to write the other special functions if the compiler-generated code does not do the right thing

    • @ZenSepiol
      @ZenSepiol  ปีที่แล้ว +1

      True, thanks for taking the time to correct the mistake!

  • @ginsonmathew186
    @ginsonmathew186 ปีที่แล้ว +1

    awesome!! lernt Something new in this video!!! thanks Zen

  • @streetzoom
    @streetzoom 2 ปีที่แล้ว +3

    Hallo, could you make a video on the topic of multithreading? I think a lot of people would be interested.

    • @ZenSepiol
      @ZenSepiol  2 ปีที่แล้ว +4

      I'll think of something this weekend :-)

  • @gamescreator5040
    @gamescreator5040 ปีที่แล้ว

    Hey! Cool and useful video, it reminds me about "Effective C++" book

    • @ZenSepiol
      @ZenSepiol  ปีที่แล้ว

      That’s one of the best books about C++!

  • @sven--
    @sven-- 2 ปีที่แล้ว +1

    Learned a few things :) thnx!

  • @stephenhowe4107
    @stephenhowe4107 ปีที่แล้ว

    1:08: The default constructor will not initialise builtin types on the stack, they will have any old value.

    • @ZenSepiol
      @ZenSepiol  ปีที่แล้ว

      True, I didn't claim that though, but maybe it was misleading.
      "The compiler is doing the right thing" meant that the copy and move constructors/assignment operators will deal with the builtin types correctly.

  • @cpp1158
    @cpp1158 2 ปีที่แล้ว +1

    Cool stuffs 🤗

  • @leafyoung
    @leafyoung 10 หลายเดือนก่อน

    Oh! primitive data member like int will not be default constructed with the default constructor with MyClass(), but with MyClass{}. So, we shall add a constructor if there is primitive data members.

  • @harisadam9929
    @harisadam9929 2 ปีที่แล้ว +3

    is it really neccesary to use the rule of 5/3/0, instead i can use the basic constructor and destructor

    • @ZenSepiol
      @ZenSepiol  2 ปีที่แล้ว +1

      I think it's a really handy rule. If I need to write one constructor, I also write the other ones. Most of the time you can get away with writing no constructor at all.

    • @harisadam9929
      @harisadam9929 2 ปีที่แล้ว

      @@ZenSepiol thanks

    • @kalvino3515
      @kalvino3515 2 ปีที่แล้ว +1

      If you have a custom destructor, you probably require a custom copy/move constructor and assignment operators. Custom constructors that doesn't require a custom destructor (Lets say, a class of a few ints) doesn't usually need to have the other functions cause the compiler will do the best thing.

  • @ajmanuel1978
    @ajmanuel1978 ปีที่แล้ว

    thanks for the video

    • @ZenSepiol
      @ZenSepiol  ปีที่แล้ว

      You're welcome