Sometimes. you just go through so much information and you understand it but you don't really understand it. Then sometimes, you stop to think and apply what you just learnt and you really understand the concept and you feel like your brain just expanded a little. This is happening too frequently since I have started watching this series.
Cherno, thanks for these videos. After taking a year off of coding after school to pursue something else, reviewing these topics from a real-world use-case standpoint makes them make a lot more sense as to what they are useful for compared to how we learned them in school. Yes, we learned them well in school, but it was rarely made clear as to what the expected usage of each topic should be. I now have a better understanding of when I should overload operators!
Basically do a: Vector2& operator++() { return (*this)--; } Vector2& operator--() { return (*this)++; } And watch the world burn. Pity that it wouldn't work.
Boy thanks again. This series has been on loop in my home for 3 months. I have been needing to override the ostream and was wondering what to do... and == TY for showing those useful tips
At [ 5:57 ] you may have noticed he used the *operator* along with the *function* for the addition (which is defined just above that code by the way). Many of you may have thought ... Hey, can it cause performance issues !? , or can lead to slightly lower performance, the answer is ... *Yes* , but it depends ... - If you're making a *specific program* (for yourself), then do make a *single operator* , not along with the function.. As, it is really unnecessary. - If you're designing your own *specific Library* , then do what *Cherno* did. It will increase the *Functionality* and the *Flexibility* of your Library class. _Long story short ... It all depends._
What kind of performance can this leads? If a compiler sees a function using const parameter it will probably optimize this inlining function which will not call a function creating another stack to the program(unless you compile with debugging flags of course).
@@yrds96 Yes I agree, but there are cases, when you have a long and complicated library to make things more abstracted (i.e. ranges-v3, or fmt), and user is heavily dependent on it. In such cases, not all compilers are able to optimize, and inline everything as we expect (I have actually seen the differences, in C++ Weekly logs). And I agree with your point as well, it really doesn't matter, if we have a simpler class. But for long and complicated ones, just imagine that not everything can be optimized, and you are still responsible for many things. :)
@@Reydriel Actually, it depends on the Compiler's Vendor, but still, there are some universal cases, where functions are inlined for sure. Like; - Calling any Function only once - Member Function, used only once - Calling the Function, having a few lines of code (roughly 3, or a little more), and called 2-3 times (or a little more). - Function with just a return statement (highly inlinable) But, sadly, there's no way to find explicitly whether the function will be inlined by the compiler. But if you meet any of the above cases, the compiler will optimize and inline your code for sure. Well, Compilers are quite trustable if you ask :)
@@TheMR-777 Looked it up, apparently you can set a flag in GCC to force inlining wherever the "inline" keyword is used, but it's not the standard behaviour
It is kinda like why Python doesn't have an easy, intended way to break out of a nested loops. The intended way is (if memory serves right) to write an if and break (or continue if you just want to skip that iteration) on EVERY SINGLE LEVEL NESTED LEVEL OF LOOPS. (rolling my eyes so hard I almost can look directly into my brain)
your channels the best c++ tutorials on you tube without a doubt but man c++ is soooo much harder than python or php my brain is literally frying listening to you
Just jump back to say thank you & clicked the thumbs up button for this video. Is there anyone like me have the same feeling: See your tutorial save me to click "x1.5" or "x2" botton.
Shivam Panwar ok lets assume that I don’t know how to write an operator overloading, I just don’t see in anyway you business in that? Please can you mind your business ignorant!
I think you do him a disservice attributing his skill to luck whilst it truly is a skill built up through practice & hard work, like any other sensible human would know and do to get proficient at anything. Live long!! Live average!! That mentality bugs me. I hate procrastination and rationalizing.
But somehow I still understand perfectly. There are so many other tutes around that speak at half the rate, yet struggle to deliver the content in an understandable format. It all seems so much more easier to learn after i watch your videos. Well done Cherno! Thanks
He built his career around making programming and he has been doing this for his whole life. It isn't luck that he is so fast, its hard work and dedication that brought him to where he is today
C++ gives you full control, which can kinda be a bad thing. Yeah, like allowing someone to write 24 bytes of random data to address location 0 of every hard drive on the system. Every hard drive. No, I wasn't that guy, I was the guy that had to see why running the application resulted in rebuilding the system disk and data drives.
11:00 I believe the compiler is able to auto generate the operator!= if you opverload the operator==, so you don't need to define your own, since the compiler just will negate the other operator you have defined.
my university professor has not shown a single code during all of his lectures, just explained dry theory. i really don't know why i go to university at all
I dont get 2 things: in for example :Vector2 operator+( const Vector2 &other) const {} 1. Why in arguments use const, and a ref instead of just const Vector2 other 2. Why you use const before opening brackets? Is it to make the return const? Why why do you want make everything const? You can't modify it later
Wouldn't having operator!=(...) return x != other.x || y != other.y be more efficient (since you don't have to check 'y' if 'x' already failed the comparison).
Hey cherno! I'm Going to be honest with you. I thought that this series is not that good and i couldn't understand anything and thought it was trash. But after looking at other series and courses, i can say that this is the best series to learn C++!
You can also watch the new Boston(bucky's c++) and apna college although cherno is also one of the best !(also it depends which one you prefer this guy is great too)
I'm actually slightly confused as to why we have the need to make these operators. Like it looks like we're just saying the same operation inside the struct method, like "position + speed" -> "coordinate + other.coordinate" - I don't see what it does differently to what was said. Also I'm not sure if I understand what we're doing, I was thinking we should multiply the speed by time to get the added position, unless our speed is actually in discreet chunks, i.e. normalized to constant time and making it directly a distance value. In any case they're two float numbers, why can't they be added without saying + means +? Because they have two values and the compiler doesn't understand what you want to add together, the x+y parameters in the position or x parameters between position and speed? Now I actually think this is a fantastic way to extend my personal math program for school work with vector addition and multiplication, I was actually just last night pondering about how to go on about it when you have things like dot and cross products.
Could someone help me with this question? After overloading, what is the order of the Arithmetic Operatoration. Does "*" still have higher priority than "+"? and Why?
Same code but in C# (if someone need it): using System; struct Vector2 { public readonly float x; public readonly float y; public Vector2(float _x, float _y) { x = _x; y = _y; } public Vector2 Add(Vector2 other) { return new Vector2(x + other.x, y + other.y); } public static Vector2 operator +(Vector2 a, Vector2 b) { return new Vector2(a.x + b.x, a.y + b.y); } public Vector2 Multiply(Vector2 other) { return new Vector2(x * other.x, y * other.y); } public static Vector2 operator *(Vector2 a, Vector2 b) { return new Vector2(a.x * b.x, a.y * b.y); } public override string ToString() => $"{x}, {y}"; } class Program { static void Main() { Vector2 position = new Vector2(4.0f, 4.0f); Vector2 speed = new Vector2(0.5f, 1.5f); Vector2 powerup = new Vector2(1.1f, 1.1f); Vector2 result1 = position.Add(speed.Multiply(powerup)); Vector2 result2 = position + speed * powerup; Console.WriteLine(result2); Console.ReadLine(); } } ------------------------------------------------------------------------------------------------------ And now passing Vector2 by reference: using System; struct Vector2 { public readonly float x; public readonly float y; public Vector2(float _x, float _y) { x = _x; y = _y; } public Vector2 Add(ref Vector2 other) { return new Vector2(x + other.x, y + other.y); } public static Vector2 operator +(Vector2 a, Vector2 b) { return new Vector2(a.x + b.x, a.y + b.y); } public Vector2 Multiply(ref Vector2 other) { return new Vector2(x * other.x, y * other.y); } public static Vector2 operator *(Vector2 a, Vector2 b) { return new Vector2(a.x * b.x, a.y * b.y); } public override string ToString() => $"{x}, {y}"; } class Program { static void Main() { Vector2 position = new Vector2(4.0f, 4.0f); Vector2 speed = new Vector2(0.5f, 1.5f); Vector2 powerup = new Vector2(1.1f, 1.1f); Vector2 result1 = speed.Multiply(ref powerup); result1 = result1.Add(ref position); Vector2 result2 = position + speed * powerup; Console.WriteLine(result2); Console.ReadLine(); } }
The operator's core of ' stream extraction / extraction operator). In the case of this video, the MAIN context is overloading the operators and their generalized implementation , not strict talking about IOSTREAM. Consequently, the notion used by Cherno is as correct as possible. Remember that IOSTREAM is just a library that you can include or not. Operators are in a different league. It's like class, namespace, int, float, template base etc. This is one of the foundations of language, with which things like IOSTREAM are built.
following what you said about having operators be relevant, I have a really tough one. In electronics, the parallel impedance of some components is calculated by taking the inverse of the sum of the inverse impedances... like 1 / ((1 / z1) + (1 / z2)). In electrical engineering, it is commonplace to express this function with the || operator. do you think I would be out of line to overload that functionality into that operator?
bool is also an operator, probably the most overloaded operators in C#. Not sure about here in c++. eg: public static implicit operator bool(Classname c){return c != null;} Also boost -> 🤮 @10:15 so basically java == trash. got it 😂
I can't seem to wrap my mind around how position "gets into" the add function. I understand they we're accessing add, and that add takes in speed, and that add returns a new Vector2, but I'm not understanding exactly how position "gets into" the function. Is this just one of those "that's how the code works" kind of things? I understand stuff like .length() returns a length, but this one is confusing
i'm working on a matrix library that works off of contiguous memory, so the basic way of accessing an element is Matrix[ columns*i + j ]; how can I implement operator overloading of [] such that I can still access it like Matrix[ i ][ j ]?
For equal operator, do we need to define an errorThreshold epislon and compare two Vector difference with that epislon? Otherwise, the comparison result may be inaccurate.
Considering that the precedence of * being executed before + was obeyed on the video (and on my computer with almost identical code), I would say "Yes".
Can someone please explain why in the return you can write: return Vector2(.....); and not: return new Vector2(...); isn't the Vector2, that's created in the function gets destructed immediately? THANKS!
yes and no... when you return Vector2(...) you are essentially returning a copy of that. The Vector2(...) created in the function is destroyed at the end of the function, however, that doesn't matter because the function is returning a copy of that . The destruction of what you created has no effect on the returned copy of the object. new Vector2(...) would create an object on the heap and return a pointer to it so the return type of the function would have to be Vector2*.
So the operator overloads only apply to things done with Vector2 but nothing else? And what's this "other" thing he keeps passing? I don't see any passing going on but his functions have parameters. Does thing1 become the "other" that is passed in something like: thing1 + thing2 ?
that's a facilities of an istream library, basically everthing is stored as a sequence of character, so here we store it in stream called "stream" and then we return the stream. Think something like std::cin, std::cin is an object of type istream.
At 5:57 (and later at 11:09), is it okay to have a method call another one instead of doing the job itself in terms of performance? Is the assembly code exactly the same or?
Yes, your point is valid, I'm thinking the same. It'll be better Performance-wise. But to increase the functionality, and flexibility of the class, he did that. It's not always recommended. - If you're making a program for yourself, then do what you said. - If you're designing a Library, then do what Cherno did. It all depends.
@@TheMR-777 yeah I would never do that. but I was wondering if the compiler is smart enough to automatically optimize the code by "reducing" two (almost) immediate context switches directly into one
@@marce3893 If you are using the *Release Mode* , then most probably it would do that (what you are saying). Because, in *Release Mode* , Compiler always finds the best way (I mean, the shortest way) possible to optimize your code, in terms of Memory, and Code length. Your point is valid, Yes it's possible.
This guy is legit, for some reason after using C++ for 4 years, i feel like i never learned the language enough
@@GH-jl2td this serie is more for beginners
I’ve heard stories of people that’s been using c++ for 25+ years and they say it’s impossible to know everything.
You learn it as you need it
@@amosreginaldjr.4200 yeah c++ is closely impossible to "master"
@@Tuho420 chill😭
@@BlueDippy why
After pausing the video 3 times, and meditating I finally got it, thank you Cherno
Ih ala, camisa do framengo brabo
soo true lol
@@lucasmorais3694 kkk, nois é hardcore
@@jerfersonmatos28 cara esperou dar 1 ano pra responder LOL
@@lucasmorais3694 é que eu nao tinha visto, mas de qualquer forma desde essa épouca que eu nao pratiquei mais programação kk, lembro mais de nada
Sometimes. you just go through so much information and you understand it but you don't really understand it. Then sometimes, you stop to think and apply what you just learnt and you really understand the concept and you feel like your brain just expanded a little. This is happening too frequently since I have started watching this series.
Cherno, thanks for these videos. After taking a year off of coding after school to pursue something else, reviewing these topics from a real-world use-case standpoint makes them make a lot more sense as to what they are useful for compared to how we learned them in school. Yes, we learned them well in school, but it was rarely made clear as to what the expected usage of each topic should be. I now have a better understanding of when I should overload operators!
Love this channel, I wish I found it 5 years ago back in university
5 years ago, he wasnt even here doing c++ tutorials
@@justasydefix6251 lol
I found him 22 years ago when I was just starting
Basically do a:
Vector2& operator++() {
return (*this)--;
}
Vector2& operator--() {
return (*this)++;
}
And watch the world burn.
Pity that it wouldn't work.
You're evil, man
Or do:
#define ++ --
@@nazeekk3873 mmmm my favourite language, C--
@Pedro Braga and mine C**
@@nazeekk3873 well your example will replace all ++ with -- . But in example above it will just call an infinite loop.
IF any of it worked :)
Learning this in my CS class kinda made me die inside. I was able to understand you much more easily, thank you!
I'm loving C++ and your series
Dude.. Don't know why I read: "I'm loving c++ and your SISTER
@@shayaxelrod7691 My sister is already taken
by me
@@puppergump4117 k
master class on operator overloading .
Note:
playback speed: 0.75;
thanksss🤧😂😅
By far my hardest C++ lesson, might have to review previous lessons
man, I must seem crazy being alone and clapping after each amazing video, TXS
Boy thanks again.
This series has been on loop in my home for 3 months.
I have been needing to override the ostream and was wondering what to do... and == TY for showing those useful tips
At [ 5:57 ] you may have noticed he used the *operator* along with the *function* for the addition (which is defined just above that code by the way).
Many of you may have thought ... Hey, can it cause performance issues !? , or can lead to slightly lower performance, the answer is ... *Yes* , but it depends ...
- If you're making a *specific program* (for yourself), then do make a *single operator* , not along with the function.. As, it is really unnecessary.
- If you're designing your own *specific Library* , then do what *Cherno* did.
It will increase the *Functionality* and the *Flexibility* of your Library class.
_Long story short ... It all depends._
What kind of performance can this leads? If a compiler sees a function using const parameter it will probably optimize this inlining function which will not call a function creating another stack to the program(unless you compile with debugging flags of course).
@@yrds96 Yes I agree, but there are cases, when you have a long and complicated library to make things more abstracted (i.e. ranges-v3, or fmt), and user is heavily dependent on it. In such cases, not all compilers are able to optimize, and inline everything as we expect (I have actually seen the differences, in C++ Weekly logs).
And I agree with your point as well, it really doesn't matter, if we have a simpler class. But for long and complicated ones, just imagine that not everything can be optimized, and you are still responsible for many things. :)
@@TheMR-777 Isn't there a way to make sure the compiler inlines the function?
@@Reydriel Actually, it depends on the Compiler's Vendor, but still, there are some universal cases, where functions are inlined for sure. Like;
- Calling any Function only once
- Member Function, used only once
- Calling the Function, having a few lines of code (roughly 3, or a little more), and called 2-3 times (or a little more).
- Function with just a return statement (highly inlinable)
But, sadly, there's no way to find explicitly whether the function will be inlined by the compiler. But if you meet any of the above cases, the compiler will optimize and inline your code for sure.
Well, Compilers are quite trustable if you ask :)
@@TheMR-777 Looked it up, apparently you can set a flag in GCC to force inlining wherever the "inline" keyword is used, but it's not the standard behaviour
Me and the bois watching The Cherno and growing brain cells
So clear, so neat and so well explained.
The best part is Cherno is criticizing the Boost.Serialization Library xD
Taking my first programming class and this series has been really helpful. Thank you.
That was some fast talking.
not that fast I still have to download and watch it on 3x seed
@@rajshekhar2953 ok
@@rajshekhar2953 Weird flex but okay
I enjoy it being fast. I hate it when tutorials/guides go super slow.
😂 you can't even get a playback on 3x seed.
"I left out operator overloading (in java) as a fairly personal choice because I had seen too many people abuse it in C++.
"
James Gosling
@Peterolen I was just quoting Gosling (creator of Java) on why java doesn't have it... which is a dumb reason. I think we agree.
It is kinda like why Python doesn't have an easy, intended way to break out of a nested loops. The intended way is (if memory serves right) to write an if and break (or continue if you just want to skip that iteration) on EVERY SINGLE LEVEL NESTED LEVEL OF LOOPS.
(rolling my eyes so hard I almost can look directly into my brain)
your channels the best c++ tutorials on you tube without a doubt but man c++ is soooo much harder than python or php my brain is literally frying listening to you
Thank you! I had difficulties with this but you made it a lot clearer.
This series is FABULOUS Cherno! You've clarified all of my doubts, thank u buddy =D
Just what i was interested in. Thanks for the great stuff
Just jump back to say thank you & clicked the thumbs up button for this video.
Is there anyone like me have the same feeling: See your tutorial save me to click "x1.5" or "x2" botton.
If people need to go to the definition of your operator to understand what it does, you're probably not doing your job.
Great video, Cherno!
I just did the calculation on the PowerUp Vector and the Speed Vector, and it turns out that PowerUp is actually slower!
Thank you
that's exactly what I need.
our instructor is just telling us non senses in school
Shivam Panwar ok lets assume that I don’t know how to write an operator overloading, I just don’t see in anyway you business in that? Please can you mind your business ignorant!
@@VideoHow And I'm pretty sure you can't structure a sentence properly because you're illiterate.
As a beginner in C++, I feel like I got into the wrong room. Anyway, you have done a great job so far.
i got stuck with an assignment for school, also using a Vector2f class in c++, this video got me started :D thx, i appreciate the help
i think you are thinking faster than other people like 1.5x speed, thats why you code so fast and can produce everything quickly. lucky for you!
I think you do him a disservice attributing his skill to luck whilst it truly is a skill built up through practice & hard work, like any other sensible human would know and do to get proficient at anything.
Live long!! Live average!! That mentality bugs me. I hate procrastination and rationalizing.
Also he edits this, and has planning time like Batman.
But somehow I still understand perfectly. There are so many other tutes around that speak at half the rate, yet struggle to deliver the content in an understandable format. It all seems so much more easier to learn after i watch your videos. Well done Cherno! Thanks
He built his career around making programming and he has been doing this for his whole life. It isn't luck that he is so fast, its hard work and dedication that brought him to where he is today
I love how he gets tired of all the card thing lul
I wish my brain would move as fast as you type
Man, you are amazing, thank you a lot for your videos.
Thanks! Love your video! and for ppl saying it's fast, just slow the video down or re-watch it. he is clear and straight to the point.
you're actually the goat oh my lord
Wonderful explanation! Truly truly wonderful explanation of a pretty tricky concept to get your head around initially. Now it all makes sense.
Looking at his videos make me feel like i am smart
I know that i don't know what he is writing. But I still know what is trying to do
I had to pause the video in the middle to like and subscribe because it's worth it.
I got so confused when I saw std::string. 🤣 This vid really saved me! Thx for the content!
I haven't understood this for a year,
and this guy explains it in 7 minutes
*_wow_*
Well it's not that complicated. You musta had some shoddy teachers.
Cherno deserves a medal!
This is so much more clear now! thanks!
It was really an informative video.Thanks Cherno😀
"What are you doing" *stares hard* LOL😆
He described that in earlier video, just a way to assign values, to make your code clearer
Operators are functions and they make code look better.
Cherno My bro your one of the best teachers I've had and I didn't even pay you shit ! love your stuff man !!!
Never fails to amaze me!
Bloody great video
C++ gives you full control, which can kinda be a bad thing.
Yeah, like allowing someone to write 24 bytes of random data to address location 0 of every hard drive on the system. Every hard drive. No, I wasn't that guy, I was the guy that had to see why running the application resulted in rebuilding the system disk and data drives.
11:00 I believe the compiler is able to auto generate the operator!= if you opverload the operator==, so you don't need to define your own, since the compiler just will negate the other operator you have defined.
I tried. My compiler will say "error: no match for 'operator!='"
when you said "bit shift" .... at first it sounded to me like "bitch shit"
If it helps from a python view, think of these operator overloading methods as deprecated methods from python (such as def __str__ and def __add__).
Binge watching these before my data structure c++ based class in Uni
some powerful stuff, really.
Are your eyes really that color, it's cool man !
very challenging stuff to understand. wow.
blows my mind
9:28 can't we just do that???
friend std::ostream& operator
Playing the video at 0.75 of the normal speed helps! Thanks TH-cam for adding that feature :D
You wanted to say: "at 1.25 of the normal speed". Then I would totally agree :]
my university professor has not shown a single code during all of his lectures, just explained dry theory. i really don't know why i go to university at all
thank you for sharing your knowledge
I dont get 2 things:
in for example :Vector2 operator+( const Vector2 &other) const {}
1. Why in arguments use const, and a ref instead of just const Vector2 other
2. Why you use const before opening brackets? Is it to make the return const? Why why do you want make everything const? You can't modify it later
Wouldn't having operator!=(...) return x != other.x || y != other.y be more efficient (since you don't have to check 'y' if 'x' already failed the comparison).
Hey cherno! I'm Going to be honest with you. I thought that this series is not that good and i couldn't understand anything and thought it was trash. But after looking at other series and courses, i can say that this is the best series to learn C++!
You can also watch the new Boston(bucky's c++) and apna college although cherno is also one of the best !(also it depends which one you prefer this guy is great too)
Thank you this helped operator overloading click
Nice one, Your T-shirt , that painting in the wall , your eyes all have the same hue of green
Is it coincident or planned :P
Looks planned
I'm actually slightly confused as to why we have the need to make these operators. Like it looks like we're just saying the same operation inside the struct method, like "position + speed" -> "coordinate + other.coordinate" - I don't see what it does differently to what was said. Also I'm not sure if I understand what we're doing, I was thinking we should multiply the speed by time to get the added position, unless our speed is actually in discreet chunks, i.e. normalized to constant time and making it directly a distance value. In any case they're two float numbers, why can't they be added without saying + means +? Because they have two values and the compiler doesn't understand what you want to add together, the x+y parameters in the position or x parameters between position and speed?
Now I actually think this is a fantastic way to extend my personal math program for school work with vector addition and multiplication, I was actually just last night pondering about how to go on about it when you have things like dot and cross products.
I'd be interested in a video about move assignment, copy assignment, assign by reference operators.
Could someone help me with this question? After overloading, what is the order of the Arithmetic Operatoration. Does "*" still have higher priority than "+"? and Why?
I was thinking the same thing. I dont know the answer but was someone did
hoping someone did
Same code but in C# (if someone need it):
using System;
struct Vector2
{
public readonly float x;
public readonly float y;
public Vector2(float _x, float _y)
{
x = _x;
y = _y;
}
public Vector2 Add(Vector2 other)
{
return new Vector2(x + other.x, y + other.y);
}
public static Vector2 operator +(Vector2 a, Vector2 b)
{
return new Vector2(a.x + b.x, a.y + b.y);
}
public Vector2 Multiply(Vector2 other)
{
return new Vector2(x * other.x, y * other.y);
}
public static Vector2 operator *(Vector2 a, Vector2 b)
{
return new Vector2(a.x * b.x, a.y * b.y);
}
public override string ToString() => $"{x}, {y}";
}
class Program
{
static void Main()
{
Vector2 position = new Vector2(4.0f, 4.0f);
Vector2 speed = new Vector2(0.5f, 1.5f);
Vector2 powerup = new Vector2(1.1f, 1.1f);
Vector2 result1 = position.Add(speed.Multiply(powerup));
Vector2 result2 = position + speed * powerup;
Console.WriteLine(result2);
Console.ReadLine();
}
}
------------------------------------------------------------------------------------------------------
And now passing Vector2 by reference:
using System;
struct Vector2
{
public readonly float x;
public readonly float y;
public Vector2(float _x, float _y)
{
x = _x;
y = _y;
}
public Vector2 Add(ref Vector2 other)
{
return new Vector2(x + other.x, y + other.y);
}
public static Vector2 operator +(Vector2 a, Vector2 b)
{
return new Vector2(a.x + b.x, a.y + b.y);
}
public Vector2 Multiply(ref Vector2 other)
{
return new Vector2(x * other.x, y * other.y);
}
public static Vector2 operator *(Vector2 a, Vector2 b)
{
return new Vector2(a.x * b.x, a.y * b.y);
}
public override string ToString() => $"{x}, {y}";
}
class Program
{
static void Main()
{
Vector2 position = new Vector2(4.0f, 4.0f);
Vector2 speed = new Vector2(0.5f, 1.5f);
Vector2 powerup = new Vector2(1.1f, 1.1f);
Vector2 result1 = speed.Multiply(ref powerup);
result1 = result1.Add(ref position);
Vector2 result2 = position + speed * powerup;
Console.WriteLine(result2);
Console.ReadLine();
}
}
well done! I finally understand!
I have to ask, what is the meaning when you write " :x(x), y(y) {} before the body of a function? is it a faster way to declare the variables?
It’s his way of writing a class constructor, it basically takes parameters on creation and sets x and y to their values
this video isnt for beginners.
I don't see where it says it is
very informative
thank you a lot
just love your lectures~!
Once you open a Cherno's video, first thing first, speed: 0.75.
0:36 Actually,
The operator's core of ' stream extraction / extraction operator).
In the case of this video, the MAIN context is overloading the operators and their generalized implementation , not strict talking about IOSTREAM. Consequently, the notion used by Cherno is as correct as possible.
Remember that IOSTREAM is just a library that you can include or not. Operators are in a different league. It's like class, namespace, int, float, template base etc. This is one of the foundations of language, with which things like IOSTREAM are built.
Actually Actually > is the extraction operator which works with std::cin.
@@jeejee4280 Yes, if we talk in context of IOSTREAM, then we call > as the extraction operator.
you literally saved my life!
How do u not make this into a recursive overload at 09:10?
following what you said about having operators be relevant, I have a really tough one. In electronics, the parallel impedance of some components is calculated by taking the inverse of the sum of the inverse impedances... like 1 / ((1 / z1) + (1 / z2)). In electrical engineering, it is commonplace to express this function with the || operator. do you think I would be out of line to overload that functionality into that operator?
bool is also an operator, probably the most overloaded operators in C#. Not sure about here in c++. eg: public static implicit operator bool(Classname c){return c != null;}
Also boost -> 🤮
@10:15 so basically java == trash. got it 😂
Thank you for share!
I can't seem to wrap my mind around how position "gets into" the add function. I understand they we're accessing add, and that add takes in speed, and that add returns a new Vector2, but I'm not understanding exactly how position "gets into" the function. Is this just one of those "that's how the code works" kind of things? I understand stuff like .length() returns a length, but this one is confusing
operator = function, neat!
i'm working on a matrix library that works off of contiguous memory, so the basic way of accessing an element is Matrix[ columns*i + j ]; how can I implement operator overloading of [] such that I can still access it like Matrix[ i ][ j ]?
you explain your video so fast
I hope you can see this comment, because I want to just say *Thank you for all these amazing videos ♥*
Why the output stream operator overloading function must be declared outside the struct scope ?
why couldn't we declare it inside the struct ?
i can't believe that this is free.
For equal operator, do we need to define an errorThreshold epislon and compare two Vector difference with that epislon? Otherwise, the comparison result may be inaccurate.
Do operator precedence rules remain the same upon being overridden, unless stated otherwise?
Considering that the precedence of * being executed before + was obeyed on the video (and on my computer with almost identical code), I would say "Yes".
Can someone please explain why in the return you can write:
return Vector2(.....);
and not:
return new Vector2(...);
isn't the Vector2, that's created in the function gets destructed immediately?
THANKS!
yes and no... when you return Vector2(...) you are essentially returning a copy of that. The Vector2(...) created in the function is destroyed at the end of the function, however, that doesn't matter because the function is returning a copy of that . The destruction of what you created has no effect on the returned copy of the object. new Vector2(...) would create an object on the heap and return a pointer to it so the return type of the function would have to be Vector2*.
can someone correct me if I am wrong but 9:17 for overloading the
So the operator overloads only apply to things done with Vector2 but nothing else? And what's this "other" thing he keeps passing? I don't see any passing going on but his functions have parameters. Does thing1 become the "other" that is passed in something like: thing1 + thing2 ?
Hey Cherno, at 9:17 , why did we say that stream
that's a facilities of an istream library, basically everthing is stored as a sequence of character, so here we store it in stream called "stream" and then we return the stream. Think something like std::cin, std::cin is an object of type istream.
your eyes are sooooooooooooooo prettyyyyyyyyyyyyyyyyyyyyy
I don't understand how operator
At 5:57 (and later at 11:09), is it okay to have a method call another one instead of doing the job itself in terms of performance? Is the assembly code exactly the same or?
Yes, your point is valid, I'm thinking the same. It'll be better Performance-wise. But to increase the functionality, and flexibility of the class, he did that. It's not always recommended.
- If you're making a program for yourself, then do what you said.
- If you're designing a Library, then do what Cherno did.
It all depends.
@@TheMR-777 yeah I would never do that. but I was wondering if the compiler is smart enough to automatically optimize the code by "reducing" two (almost) immediate context switches directly into one
@@marce3893 If you are using the *Release Mode* , then most probably it would do that (what you are saying). Because, in *Release Mode* , Compiler always finds the best way (I mean, the shortest way) possible to optimize your code, in terms of Memory, and Code length. Your point is valid, Yes it's possible.
why are u calling them by refrence? it's not a friend operator so do we need to call it by refrence?
Is the purpose of returning stream in the
Yeah, that's the reason, chaining is a really useful thing, especially you are going into functional programming
Thanks your question/answer cleared my dubt.