im guessing the compiler wouldn't want to cast the actual data because there is a difference in size but will totally allow the cast to be done with pointers since well... they're just pointers. Thats the best i could come up with.
*this is ShapeCRTP T is Square So you cannot cast from a templated object to a non templated object. But “this” is a pointer You can always cast from a pointer to a pointer, the difference rises when you dereference the pointer.
_Says is going to make an abstract base class, proceeds to not make an abstract base class and then instantiates it_ If you had actually made an abstract base class, the code wouldn't have compiled.
This usage of CRTP is not correct. First it creates unnecessary bloat code. Second this is being taught with an idea behind polymorphism which is very wrong if CRTP is used this way since polymorphism was just created for the purpose it has. Consider looking at CRTP as a mixin but compilated and provided through a particular interface.
I went with the polymorphic copy construction example from Wikipedia, as I felt that for people new to programming that it would be the easiest to understand and the most immediately relatable. It's still useful in this way for removing code repetition. I do however appreciate the fact that you were expecting to see the static polymorphism use case, and you're not the first person to mention it.
The main advantage of using CRTP is the LACK of virtualisation. What you've shown is a special case in which the virtualisation is beneficial, but this is not the main point of the CRTP. I have to thumb it down for being somewhat misleading. People are trying to learn programming from you, and I am sure you have good will, but if you want to cover such broad topic, don't make a short video about it. It's like explaining to someone how to use a car by pouring fuel to the tank - you show a useful part of maintaining the car, but where's the driving part?
Goddamn this is the clearest explanation of crtp I've ever seen. 👍👍
Man. Someone is loving their cherry blues in the background
Man, your examples are really great.
Wow I just finished watching your c++ series and it's just spectacular!! I wish you would do more videos like these...
I second this!
Your explanations are excellent! Please make more of these!
Excellent as usual!
You can even create a paid course on Udemy. I won't mind buying one access.
these videos are a hidden gem.
Hello very nice Videos, Please make more videos, Thanks
CRTP ----> Whenever a class/struct seems to use something which is templatized on itself.
Hello Sir, I really like your way of explaining these advanced C++ topics and would love to see you in my subscription box again! (:
+1!
Great series. Keep up your good work
Hello, very interesting and useful content. Please make a tutorial on multi-threading and parallel programming.
thank you so much for making this so easy to follow
Great videos man!
Great Video good work.
Nicely done!
Please make more videos!! You`re a good Thanks.
Really Cool Explanation.
Can someone explain the memory deletion process her ? how should clean what have been created inside the template?
The Clone() function returns a pointer to a new Shape, which you can call delete on later on. Its not responsibility of the class to deal with that.
but it is still virtual, what's point ? it only helps you write code like macro does
Nice video.
Why is it not possible to Change the line to:
virtual Shape* Clone() override { return new T(static_cast(*this); }
?
im guessing the compiler wouldn't want to cast the actual data because there is a difference in size but will totally allow the cast to be done with pointers since well... they're just pointers. Thats the best i could come up with.
*this is ShapeCRTP
T is Square
So you cannot cast from a templated object to a non templated object.
But
“this” is a pointer
You can always cast from a pointer to a pointer, the difference rises when you dereference the pointer.
_Says is going to make an abstract base class, proceeds to not make an abstract base class and then instantiates it_
If you had actually made an abstract base class, the code wouldn't have compiled.
This usage of CRTP is not correct. First it creates unnecessary bloat code. Second this is being taught with an idea behind polymorphism which is very wrong if CRTP is used this way since polymorphism was just created for the purpose it has. Consider looking at CRTP as a mixin but compilated and provided through a particular interface.
What's the point of using CRTP if you are gonna make virtual function calls?
I went with the polymorphic copy construction example from Wikipedia, as I felt that for people new to programming that it would be the easiest to understand and the most immediately relatable. It's still useful in this way for removing code repetition. I do however appreciate the fact that you were expecting to see the static polymorphism use case, and you're not the first person to mention it.
oneproduct appreciated your reply and explanation. Thanks for your sharing. Hope to see more videos on C++ from you in the future.
Why does he have such a low subscriber count
The main advantage of using CRTP is the LACK of virtualisation. What you've shown is a special case in which the virtualisation is beneficial, but this is not the main point of the CRTP. I have to thumb it down for being somewhat misleading. People are trying to learn programming from you, and I am sure you have good will, but if you want to cover such broad topic, don't make a short video about it. It's like explaining to someone how to use a car by pouring fuel to the tank - you show a useful part of maintaining the car, but where's the driving part?
this is simply mess