While the "senior" branding is a bit hyperbolic for such a fundamental concept, the video effectively distills a core performance pillar into an accessible lesson. It serves as a necessary reminder that mastering memory efficiency is the first step toward professional-grade C++ development.
Approfondir
Prérequis
- Pas de données disponibles.
Prochaines étapes
- Pas de données disponibles.
Approfondir
Why Senior Engineers (Almost) Never Pass by Value in C++Ajouté :
In the previous video, we went over what references are. So, a reference is a way to create an alias for another variable.
If we call an variable int A and we create a reference with this ampersand symbol, B actually holds the address of wherever A is. And when you set the value B equals 5, you would get that A equals 5 and B equals 5. Now, this idea by itself might not make that much sense, but it does make a lot of sense if you realize how it's used in functions. And that's where passing in variables by reference comes into play.
So, in this video, I wanted to go over what pass by reference is, when you might use that, how that helps improve your performance by not having to do extra copies, as well as how you can use it to modify data.
Here, I've got two functions. The first one is add one and the second one is add one by ref. The first one is passing the variable by value, which essentially means that it's copying the value. But, the interesting thing happens when you pass in a variable by reference. So, here, we're essentially doing the same thing that we did before with the int ampersand B equals A, right? Except we're doing this inside of a function.
When we do this, X becomes an alias to the variable A. We've got this variable A here. So, when we're using this function right here, the X value inside of that function becomes an alias to the value A, which is inside of the scope of int main. So, if you were to increment X inside of this add one by ref function, which creates this reference, it also increments A inside of main. So, I'm going to show you guys this by printing out the values. And here, you could see that in the first iteration in add one, we're passing it in by value. So, after it's completed, A does not actually change inside of int main. But, when we're passing it in by reference, it does get changed. And now, the real question is, why do we have this idea of passing in something by reference?
So, first of all, there's actually two benefits to passing in something by reference. Now, the first is to avoid copying large amounts of data, and the second is if you actually want to change the original value of the data. So, these are both the perks of using pass by reference. And now, the question is, when do these actually come into play?
Let's just say you're actually writing a program that people use, like how useful is this technique?
Suppose you've got a vector V. It just has a bunch of integers in it. And all you're trying to do is print that vector. Don't worry too much about what's inside of this. Maybe there's just a for loop that goes through and does std::cout to print the vector. That doesn't really matter. What does matter is how you pass that vector in to the function. So, there's a couple different ways to do this. And the first way that most people might do, if you're just programming C++ and you're just getting started, is you might do something like this. Say you create a vector like 1 through 10. And then, you call this function in int main. What C++ does is is actually, it constructs that same vector again because it has to copy all that data into this vector. Because it doesn't know what you're going to do with the vector inside of this function, right? Because it you haven't told me that you want it passed in by reference.
So, I'm just going to copy the entire thing. And now, you can imagine if this vector's like of size three or four variables, you might be like, oh, whatever, that's totally fine. But, imagine the vector has like 100 or 200 or 300 variables. All that needs to be copied in. And that's when you start running into some problems. And this is why passing in something by reference is actually very useful. Because what this is telling C++ is, you know, don't worry too much about copying all that data.
I'm just going to, you know, just pass it in by reference, so you don't have to do all that actual work of copying in like, let's say the 100 or 200 or 1,000 variables in that integer vector. Just pass it in. Now, at this point, you might be thinking, hey, look, this is good enough. We don't need to do anything more. But, there is one more thing that actually is useful to do. And that is to drop in a const right here.
What does it mean to pass in const?
Let's assume that that some other team is working on this function, right?
Like, they're just printing out this variable. I don't know what they do inside of it. Maybe they do other stuff.
I don't care. But, the point is, another team is working on this. Now, when you're calling something like a print function, it actually might be confusing for you to pass in a reference to the vector. Maybe you don't want this vector changed. But, if you pass it in like this, right? And say like the other team accidentally modifies it, that might mess up your code. In these types of situations, it's actually beneficial to write const. So, if you're ever just passing in variables by reference, you most likely want to do it in const because in many cases, you actually don't want to change what the value is inside of V. What this does is it tells the compiler, if you try to change anything in V, I'm going to throw a compiler error. But, it also does something else is it tells the end user, essentially me, right? The person calling this function, nothing's going to change from V. Like, you, the engineer who's working on a different team, who's created this print V function, has told me this is a const.
So, I personally know that nothing's going to happen to it because, you know, they just gave me this API and I know that they're not going to change it. And if they do change it, not just me, but the compiler's also going to be confused, right? So, both of us are going to be confused. So, in many cases, you're going to want to, if you pass in something by reference, you also want to drop in that const right there. But, in some cases, you don't. And let's see when that's useful.
Suppose you've got a function where you're iterating through the vector and adding one to each value in the vector, something of that nature. In that case, you're going to want to pass in the reference to that vector, and you're going to want to make sure that it's not const. Because if it's const, then the compiler will throw an error. If you're copying the vector, the copy is not going to be reflected inside of the parent function, which is actually calling this function. In this situation, you're actually just going to want to pass in a reference.
Now, obviously, add one here is a little bit of a contrived example, but, you know, you could imagine that you have something like normalize. Maybe it takes in a vector 3.
And in this case, you actually want to pass it in by reference. And, you know, you might actually want to change the value inside of this vector because it probably is slower if you decided to copy the vector, then normalize it, and then return a vector 3 which you set the new vector to. Obviously, that approach is going to be a lot slower than just normalizing the vector in place. So, in certain situations, you're going to want to use this approach. And that's what I wanted to go over in this video. What does it mean to pass in a variable by reference, how that creates an alias, and how that updates the variable itself, how in many cases, if you don't actually want to change the data, then you want to use a const reference because that tells you that, you know, it's not going to be modified. And of course, in some situations, you're actually going to want to modify it, in which case, both of these options are incorrect, and something like this is the correct approach. If you've made it this far, hit the like button, and consider subscribing for more C++ deep dives just like this. Thanks for watching, and I'll catch you guys next time.
Vidéos Similaires
resume fixed instantly 😭 Comment “app”andI’ll sendyou the link #parakeetaipartnership #resumetips
Ritcareer
686 views•2026-05-31
Re: 🗣️📍theprophedu📍2026 GST 103 CLASS (E-EXAM REVISION)
theprophedu
636 views•2026-06-04
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation💯✅
LearnwithSahera
1K views•2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 views•2026-05-29
Search Algorithms Explained in 60 Seconds! 🤖💨
samarthtuliofficial
218 views•2026-06-01
Making Minecraft Clone with C++ & Raylib
PecaCSLive
686 views•2026-06-04
People of Game of Thrones using JavaScript DOM
AltCampus
296 views•2026-05-30
Instagram accounts got PWNed
EricParker
13K views•2026-06-03











