Pass By Reference | C++ Tutorial

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

Thank you sir for the most clearest and understandable explanation.

olegparamonov
Автор

This was very easy to understand, much appreciated.

Garrison
Автор

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

theea
Автор

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

Amber-leds
Автор

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.

shephardmukachi
Автор

Best explanation... thank you so much!!

obelisk
Автор

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.

cain
Автор

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.

Klusio
Автор

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
}

aboutmount