How To Implement Method Chaining | C++ Tutorial

Sdílet
Vložit
  • čas přidán 29. 08. 2024
  • How to implement method chaining in C++ so we can call object methods like: obj.method1().method2().method3(). Source code: github.com/por.... Check out www.portfolioc... to build a portfolio that will impress employers!
    Interesting Method Chaining Research Paper: "An Empirical Study of Method Chaining in Java"
    Link: static.csg.ci....

Komentáře • 20

  • @ieduardoms
    @ieduardoms Před rokem

    I am watching these videos to learn C++ OOP, but I am learning even more. Thank you very much.

  • @thee3a
    @thee3a Před 2 lety

    This saved me. You explain concepts so clearly. Thank you so much!!

    • @PortfolioCourses
      @PortfolioCourses  Před 2 lety +1

      You're welcome Thea! 😀 I'm glad to hear it helped you out and that you're enjoying the explanations.

  • @user-yk2dc6yr7q
    @user-yk2dc6yr7q Před 9 měsíci

    3:16 my timestamp to implement method chaining

  • @atharvarai5239
    @atharvarai5239 Před 2 lety

    Hey awesome video! I finally got it. Just to confirm, does the & after Rectangle in function definition(Rectangle&) signify that the return type of this particular function is a pointer reference or address!?

    • @PortfolioCourses
      @PortfolioCourses  Před 2 lety +1

      Great to hear that Atharva! And yes, it signifies that the return value is a reference. :-)

    • @atharvarai5239
      @atharvarai5239 Před 2 lety

      @@PortfolioCourses ❤️❤️

  • @muhammadbasim146
    @muhammadbasim146 Před rokem

    which c++ compiler are you using on mac?

  • @shafayet0198
    @shafayet0198 Před rokem

    the function returns the current object, so why we have to add '&'(address of the object) on the function return type?

    • @PortfolioCourses
      @PortfolioCourses  Před rokem

      Great question Akash. :-) That's because we are returning a reference to the current object. This video discusses reference variables and returning a reference: czcams.com/video/e3DN1RaYVYQ/video.html. Returning a reference to the current object allows the next method in the chain to be called on the same object with .method_name().

  • @mytech6779
    @mytech6779 Před 2 lety

    ( c++ 101 perspective) In a normal getter method you don't return the class object, it would just be something like ` int getLength(){ return length ; } `
    What is the purpose of returning a reference to a whole class instance `Rectangle&` ? Also this example would be much more practically helpful example if the member data was private as is more common.

    • @PortfolioCourses
      @PortfolioCourses  Před 2 lety +1

      The video is about how to implement method chaining in C++, where we have:
      obj.method1().method2().method3()
      as is popular in some languages/frameworks. The idea is that we can run several methods (method1(), etc.) of the same object ("obj") in sequence in a single statement. In order to do this, you need to return a reference to the object itself, so that's why we do that.
      Making member variables pubic or private is not important for this example. When we use the private access specifier "practically" we do so to make it harder for programmers to access the data outside the class definition, that is not necessary or helpful in this example and doing so would not make it more "practically helpful".

    • @mytech6779
      @mytech6779 Před 2 lety

      @@PortfolioCourses Thanks for clarifying the reason for returning a self reference.
      I mentioned the private/public thing because I was trying to understand how a method that is normally not void type, like a getter, could be written for use in a chain and yet still be useful as a getter that would return an int. A method can't return both a self reference and an int at the same time and making the data public brushes by this, it just seems a bit hand wavy to say it is trivial yet it wasn't done.Maybe two methods(overloaded?) are needed.
      I know this vid is only about how, not should, but I get the impression that chaining is more of a novelty with a low return on investment.(says the guy typing three paragraphs on YT...)

    • @PortfolioCourses
      @PortfolioCourses  Před 2 lety +1

      @@mytech6779 Oh we wouldn't have a 'getter' in a chain is the answer than I suppose, just because the nature of the chain doesn't really allow for that. It's "should" case is situations where we want to make a sequence of actions or state changes that are perhaps related in some way, so that having them on one line actually improves readability. It's definitely a 'niche use case' though, it's not something that's advisable in the vast majority of situations. 🙂

    • @mytech6779
      @mytech6779 Před 2 lety

      @@PortfolioCourses Sounds reasonable to me.

  • @didiTchu
    @didiTchu Před 2 lety

    rectangle is somewhat dull why not implementing exiting classes...

    • @PortfolioCourses
      @PortfolioCourses  Před 2 lety +1

      The example is simple because it's just meant to be a basic tutorial to show how method chaining works. :-) Maybe one day I can do more complicated examples, but method chaining isn't used as much in C++ as it is in other languages so I don't know.