Design Patterns - Factory Method Pattern Adding More Power to Count Allocated Objects in C++

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

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

  • @georgiosdoumas2446
    @georgiosdoumas2446 4 หลายเดือนก่อน +1

    11:34 , I think that destructor should not have any parameter list!

    • @MikeShah
      @MikeShah  4 หลายเดือนก่อน

      Indeed -- copy & paste error without actually testing (just in the 'discussion' or 'sketch' portion of the video). I'd need to think a bit about a use case for a parameter in a destructor -- perhaps for control flow on how you'd like to perform deletion for instance -- but the mechanism may not work as well as 'defer' or 'scope(exit)' in other languages.

  • @Maximus98245
    @Maximus98245 7 หลายเดือนก่อน +1

    Hi Mike, great video! Could we have not used the reference count of the shared pointers to tell us how many active objects there are? Or maybe the reference count of the weak ptr associated with the shared ptr ? Thanks!
    auto ptr1 = std::make_shared(10);
    {
    auto ptr2 = ptr1;
    std::cout

    • @MikeShah
      @MikeShah  6 หลายเดือนก่อน

      We can utilize use_count to tell how many times the pointer is referenced in a single-threaded environment (keep in mind, multithreading will otherwise not make this or 'expired' reliable)

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

    can you explain how to further code the fragment with adapter?

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

      At some point I'll cover some other patterns like adapter in this series.

  • @1981Praveer
    @1981Praveer 11 หลายเดือนก่อน +1

    @9:35 can't we delete by overloading the delete operator? something like:
    void operator delete(void* memory){ free(memory);}

    • @MikeShah
      @MikeShah  11 หลายเดือนก่อน

      Could certainly do something like that to track allocations.

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

    Is the wrapper the best approach for this problem? Could you store them in a container that gets created with the constructer and push back the shared_ptr when the CreateObject function is called? Then you could use the size function from that container?

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

      Could also use a container to count the number of objects created. I think I would improve the above by using an atomic to keep count at the least.

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

    Hi, Mike!
    Awesome tut, I have one question: on 3:23 you created copy-constructor in private section of "factory" class, but what is the difference with explicit restriction on copy constructor on next line:
    FactoryGameObjects(const& FactoryGameObjects o) = delete;

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

      Effectively the same idea -- one is the pre c++11 way of avoiding copies, and 'delete' is the C++11 and beyond way of doing things.

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

      I spotted your case in one of SDL examples(file or asset handler maybe) , so this question appeared in my mind.

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

      @@heyheyheyhoev419 Ah great! :)