Singleton Design Pattern | C++ Example

แชร์
ฝัง
  • เผยแพร่เมื่อ 6 ก.ย. 2024
  • How to implement the singleton design pattern in C++. This implementation is thread-safe and uses lazy instantiation. Source code: github.com/por.... Check out www.portfolioc... to build a portfolio that will impress employers!

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

  • @alexplaytop
    @alexplaytop 5 หลายเดือนก่อน +2

    Easy understand even for Juniors, thx MAN...

  • @dev-evandro
    @dev-evandro ปีที่แล้ว +4

    Very very well explained! Explained clearly and at an ideal speed of speech. It is very pleasant to listen to and you can directly comprehend the content conveyed. Thanks for the great video!

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

      Thank you for sharing such kind positive feedback, and you're very welcome! 🙂

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

    I have checked out a lot of sources and channels but none of them explained as good as this one. can you by any chance make a data structures series in c++? it would be bomb!

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

      I'm glad you enjoyed the video! :-) And hopefully one day I can do a data structures series in C++.

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

    Great and well explained video. Thank your for your sharing. I hope you could add the rest of design patterns. It would be very helpful.

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

      You’re welcome! :-) I do eventually want to cover more design patterns in C++.

  • @bimblebom
    @bimblebom 8 หลายเดือนก่อน

    Your lessons are really brilliant! Short, terse, succinct with clear examples. Are you on Patreon?

  • @amortalbeing
    @amortalbeing 5 หลายเดือนก่อน

    great job ❤

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

      Thanks I’m glad you liked it! :-)

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

    good stuff Kevin!

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

    Thanks for the video...it really helps.:)

  • @deltanine1591
    @deltanine1591 27 วันที่ผ่านมา

    Thanks bro

  • @CSEAJMALAKRAMS
    @CSEAJMALAKRAMS 9 หลายเดือนก่อน

    Thanks ❤ keep it up & plz post other important design patterns tooo

    • @PortfolioCourses
      @PortfolioCourses  9 หลายเดือนก่อน +1

      You’re welcome! :-) What other design patterns would you like to see covered?

    • @CSEAJMALAKRAMS
      @CSEAJMALAKRAMS 9 หลายเดือนก่อน +1

      @@PortfolioCourses decorator, command patterns and some important design patterns.. And Thanks for the response 🙌💫

    • @PortfolioCourses
      @PortfolioCourses  9 หลายเดือนก่อน +1

      @@CSEAJMALAKRAMS Thanks for the feedback! 🙂

  • @Solo6R
    @Solo6R 8 หลายเดือนก่อน

    Why make the constructor protected instead of private? I always made my singleton class constructors private and have had no issues. Maybe protected is doing something else I don't know?

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

      Not really sure either, constructors aren't inherited so idk.

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

    Hi sir from week I have been following your channel
    Recently I come across why globally pointers allocate space in data segment in memory map..

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

      Thanks for following the channel Rama. Is that a topic you think I should make a video on? Do you have a link to the article or video that you came across? :-)

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

    Great video. Are there any plans to do any more design pattern videos? I would love to see more

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

      I would love to cover all the classic design patterns in C++ and Python. Exactly when I'll get to doing more, I'm not sure. :-) This video has started to get some great feedback and viewership over the last while though, which is really encouraging to make more design pattern videos.

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

    Thank you 😊

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

    I have a doubt acc to definition n code; the second creation of object singleton2 should not be correct or possible.
    What is the reason we don’t get any such error?

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

      Making the copy constructor a deleted function is what prevents the Singleton from being copied by using an assignment operator like this :Singleton singletonN = singleton1;. If the copy constructor is not a deleted function, then the assignment statement will be OK, and we will get a copy of the singleton using the default copy constructor.

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

    Hello, this is nice example, could you make one video? " Multiton pattern on C++"

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

      That is a great idea for a video, I have added to my list of ideas. :-)

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

      @@PortfolioCourses Ohhhh, thank you very much

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

      @@osnovan7169 You're welcome! 🙂

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

    hey Kevin.
    at 4:49, variable singleton1 is also a reference to a Singleton object. But get instance also returns a reference, so you returining a double reference at the end? Im getting kinda confused

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

      The get_instance() function returns an instance, and singleton1 is an instance, so the types make sense. We're not returning a 'double reference' or anything like that.... get_instance() just returns a reference to that static instance variable and singleton1 will then become a reference (i.e. synonym) to that same variable. Reference variables definitely aren't straightforward, maybe this video will b useful: th-cam.com/video/e3DN1RaYVYQ/w-d-xo.html.

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

      @@PortfolioCourses thanks master

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

      Haha you’re welcome! :-)

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

    best, though you hasn't explained the syntax in the multiple deleted operators

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

      In videos on more advanced topics like this I don't always explain certain things due to time constrains. :-)

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

    What does "= default " do ?

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

      It makes a defaulted function, this video explains the concept: th-cam.com/video/OupyUY7FKUI/w-d-xo.html. :-) There is also deleted functions too: th-cam.com/video/-PQRjsX_1i8/w-d-xo.html.

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

      @@PortfolioCourses thanks - so default is used when NO Parameters are sent ?

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

      In this example, yes.
      Though this is a very minor point, technically what we pass the function are called "arguments". The "parameters" are the variables that those arguments are set to. So if we have:
      void func(int x) { ... }
      func(5);
      Then x is the parameter and 5 is the argument.