Correction: I've missed a prefix in one example. At 01:53 there should be a Foo:: prefix for the Bar() function definition in the bottom of the screen. Thanks for watching and keep that feedback coming! 🙏 Enjoy!
4:52 One example where calling a static function on a class object is when the class is templated and you already have a object instance of that class template. In this case if you call the method using the `::` syntax you're violating the DRY principle because you're repeating the class template with all of its template arguments. For example: MyClass obj = ...; //do something with obj MyClass::static_method(); //you're repeating MyClass which is prone to typos Instead if you use the object there's no repetition: MyClass obj = ...; //do something with obj obj.static_method(); //fine, it calls MyClass::static_method() And in the future, if you want to change the template arguments, let's say from MyClass to MyClass you only have to change it in one place. Of course, you can always do `using MyClassInst = MyClass;` and then use the alias instead of the full template instantiation but it requires writing more code.
I see the point to a degree. But as you say, I’d use a using there probably. Also, if we get an auto variable in a lambda or smth, so that we don’t have its type spelled out, I’d still use a using and the :: to be more explicit in my intent.
Ok, I've figured out why I did not catch the error with my automatic validation of all the code. The reason was that for whatever reason I used the -c flag in all my code snippet validation commands. Probably a copy-paste mistake. 🤦♂ Anyway, this is now fixed in the script that corresponds to the lecture and the correction is added to the video. Unfortunately this is as good as it gets without re-uploading the video. Please tell me what you think!
I’m not sure. When it comes to functions that belong to a class I’m used to calling them methods or functions interchangeably. But honestly I’ve never thought about it much. 🧐
Well, that’s what I tried to do in the video 😅 Anyway, in my understanding static for classes exists just to aid encapsulation, meaning that we can bundle any static data or functions into a class if they will ever be used in relation to that class. This helps readability and “scoping” of these variables and functions. Does this make any sense?
Correction: I've missed a prefix in one example. At 01:53 there should be a Foo:: prefix for the Bar() function definition in the bottom of the screen.
Thanks for watching and keep that feedback coming! 🙏 Enjoy!
Great series of lectures. Really valuable even if you have been working with C++ for many years.
Thanks for telling this! Helps my motivation a lot! 🙏
4:52 One example where calling a static function on a class object is when the class is templated and you already have a object instance of that class template. In this case if you call the method using the `::` syntax you're violating the DRY principle because you're repeating the class template with all of its template arguments.
For example:
MyClass obj = ...;
//do something with obj
MyClass::static_method(); //you're repeating MyClass which is prone to typos
Instead if you use the object there's no repetition:
MyClass obj = ...;
//do something with obj
obj.static_method(); //fine, it calls MyClass::static_method()
And in the future, if you want to change the template arguments, let's say from MyClass to MyClass you only have to change it in one place.
Of course, you can always do `using MyClassInst = MyClass;` and then use the alias instead of the full template instantiation but it requires writing more code.
I see the point to a degree. But as you say, I’d use a using there probably. Also, if we get an auto variable in a lambda or smth, so that we don’t have its type spelled out, I’d still use a using and the :: to be more explicit in my intent.
Really good video ! keep it up ^^
Thanks so much! Glad you liked it! 🙏
Great content. Please, record more videos!
Thanks so much!
I used too much inside out side const constexpr; it’s helpful
Hmm, I don’t fully understand what you want to say, could you maybe rephrase?
Superb video
Thank you so much 😀
1:53 should be void Foo::Bar() {...}
Oh! You’re right! I wonder how that slipped through my automatic checks… I’ll add a correction!
Ok, I've figured out why I did not catch the error with my automatic validation of all the code. The reason was that for whatever reason I used the -c flag in all my code snippet validation commands. Probably a copy-paste mistake. 🤦♂ Anyway, this is now fixed in the script that corresponds to the lecture and the correction is added to the video. Unfortunately this is as good as it gets without re-uploading the video. Please tell me what you think!
You sure there are (static) methods in
C++? ;)
Hmmm, I'm not sure I'm getting what you are asking 😅
@@CodeForYourself aren’t they just called functions? The word method is normally not used in C++ (nitpicking)…
I’m not sure. When it comes to functions that belong to a class I’m used to calling them methods or functions interchangeably. But honestly I’ve never thought about it much. 🧐
So funny. Just tell as why it exists and we will understand. Why would you use it over regular functions for instance.
Well, that’s what I tried to do in the video 😅 Anyway, in my understanding static for classes exists just to aid encapsulation, meaning that we can bundle any static data or functions into a class if they will ever be used in relation to that class. This helps readability and “scoping” of these variables and functions. Does this make any sense?