Pass By Reference | C++ Tutorial

Поделиться
HTML-код
  • Опубликовано: 12 фев 2022
  • How to use pass by reference in C++ function parameters in order to alter the values of variables in the calling function. An explanation of how pass by reference works with objects is also included. Source code: github.com/portfoliocourses/c.... Check out www.portfoliocourses.com to build a portfolio that will impress employers!
  • ХоббиХобби

Комментарии • 26

  • @Garrison86
    @Garrison86 Год назад +5

    This was very easy to understand, much appreciated.

  • @pasinduwathsara7566
    @pasinduwathsara7566 2 года назад +3

    Clear and smooth explanation...!!!

  • @Amber-le8ds
    @Amber-le8ds 2 года назад +1

    Thank you this helped me fix my code for an assignment.

  • @thee3a
    @thee3a Год назад +1

    I couldn't find a C++ course on your website. I hope you plan to make one! Your C++ tutorials are the absolute best.

    • @PortfolioCourses
      @PortfolioCourses  Год назад +1

      I don’t have a full C++ for beginners course yet, but I’m really hoping to make one this year Thea! :-)

    • @thee3a
      @thee3a Год назад +1

      @@PortfolioCourses I really hope you do!! You explain things so clearly. I hope you make some tutorials about multithreading and reference counting 🤯 in the future as well!

    • @PortfolioCourses
      @PortfolioCourses  Год назад +2

      Cool, thanks for the ideas! :-)

  • @obelisk7393
    @obelisk7393 Год назад

    Best explanation... thank you so much!!

  • @shephardmukachi2817
    @shephardmukachi2817 9 месяцев назад

    Hi. Something has been mind boggling me for a few days and I thought of you. I'm developing a C dll where a few functions receive structs by reference(pointer). Let's say I want to bind the passed struct object to the DLL (that is have a pointer in the DLL references the address of the passed struct instance) so that changes made in either DLL or client program take immediate effect. Many sources I read have shown examples of DLL functions accepting arguments by pointer { of the form bind(struct val*) }. Recently through trial-and error I realised that I can actually make the DLL function accept by reference - to my surprise, however now my question is, if DLL function accepts by pointer, how do you get the address from it, to point to it's own pointer (bind), and also if passed by reference, what is the procedure for doing the same? I hope I'm making sense. Also, if I pass a pointer (the actual integer/long value, how can I convert it to an actual pointer then deference it? I know I have asked a lot, but you have way more knowledge and understand than I do, and was hoping you could help me get some clarity. Thanks in advance.

  • @cain372
    @cain372 2 года назад

    I'm a web developer, but I'm interested in the C and C++ programming languages ​​because most of the time if you want to learn a programming language, it's better to know the C programming language.

    • @PortfolioCourses
      @PortfolioCourses  2 года назад

      I think learning a lower level language like C can have benefits to all programmers, by learning how things work at a lower level and by learning a new way of thinking and how to solve problems. :-)

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +1

      @Aboubakr Hollanda We've all got our favourites - I definitely love C++. :-)

  • @Klusio19
    @Klusio19 Год назад

    I think I understand passing by value and passing by reference, but I have one question (so actually, maby I don't understand it fully haha):
    In 2:48, when You used function using pass by reference, the "&" symbol and then x, means that we are giving the function exact address in memory where that variable "lives" and then we are operating on her etc. And my actual question is about calling this function. Shouldn't we first create pointer to the "a" variable, and then pass that pointer to the function? I mean, I see it's working when we just provide "a" variable, but I'm mot really sure why.

    • @Klusio19
      @Klusio19 Год назад

      Or then maby I don't understand pointers fully haha

    • @PortfolioCourses
      @PortfolioCourses  Год назад +2

      Great question! 🙂 So "reference variables" and "pass-by-reference" is a different concept than pointers, even though they work somewhat similarly and use the same & symbol (which makes things confusing!). The &x does not mean we are giving the function the memory address, all it means is that the parameter is a "reference variable". And a reference variable basically becomes "another label for the variable it is set to". This video on reference variables might be helpful for you: ruclips.net/video/e3DN1RaYVYQ/видео.html.

    • @Klusio19
      @Klusio19 Год назад

      @@PortfolioCourses ooooh, then that makes sense (the "&" symbol) why I confused these both concepts, I haven't watched that video yet, I'm watching all your C++ videos in chronological order. Gonna watch that right now! Thank you very much for your answer! 😀

    • @PortfolioCourses
      @PortfolioCourses  Год назад +1

      @@Klusio19 You're welcome! 🙂

  • @ericahays4281
    @ericahays4281 Год назад

    finally jesus christ i get it.

  • @didiTchu
    @didiTchu Год назад

    why dont you say ampersand

    • @PortfolioCourses
      @PortfolioCourses  Год назад +2

      No real reason, I usually just call it the 'and' character because the & symbol standards for 'and'. And I guess it's shorter than ampersand. :-)

  • @about2mount
    @about2mount 5 месяцев назад

    When you spoke of shadow copies in passing non referenced variable objects. There exists another way. I do it by placing my counts and temporary buffers as variables in the header files above all of my data types and above all the functions to both access and change them without reference.
    It is far more easier for the compiler to reference them as global-like and do so without the confusion. A class with a counter placed above it can still access and change that global variable because doing it your way is also defining a reference, however its being done after compile time and is way more costly.
    The only time I ever use a reference's is when passing an array, a vector, a map, a struct array or a class container as an array. Then it becomes valuable in programming.
    int bag_cnt = 0; // OUR COUNTER.
    class Bagger {
    public:
    int bid; // WHERE WE STORE THE INCREMENTED COUNTS.
    int bdata;
    void assign_data(int dat) {
    bid = bag_cnt;
    bdata = dat;
    bag_cnt++ // WE INCREMENT IT HERE.
    };
    };
    int main () {
    Bagger b[200]; // we assign the class as a data type(w/it is one) with an array count of 200
    b[bag_cnt].assign_data(44); // 0
    b[bag_cnt].assign_data(93); // 1
    b[bag_cnt].assign_data(23); // 2
    }