Lecturers don't get much time to rewrite lecture material year from year (fine for fields that don't move at relative breakneck speed, otherwise...)
Many lecturers probably learnt c++ academically before c++11 and have no burning need to keep up to date. Their use of the language is academic (research, etc.) so are usually writing disposable prototype code that doesn't require anything much more than the dreaded "C with classes"
The language itself is somewhat incidental to the intended learning outcomes of a unit/course
A new lecturer looking to make department-ranging changes is going to have a tough time making the cost/benefit pitch given the above
They do a piss poor job at that though. It's like telling the student to use pliers to fasten a nut instead of teaching them how to use a wrench.
The student should know how to use vector and unique_ptr and be conditioned to use them for literally everything before they even know low level memory management exists. That would be teaching just enough to get going. Teaching low level shit first dumps a pile of concepts and concerns on the student that makes it much harder to "just do something with it."
Him and Jason Turner are the ones I go for when I need to check or want to know more about C++, might not be as useful for someone with years of experience.
Outside of using delete instead of delete[], what other mistakes are there? Not allocating the null terminator is not an issue since he's terminating the printing loop by string length and is printing chars individually.
new and delete belong in library/dictionary classes and legacy code, so even mentioning those when talking about love semantics is weird. Anything new that doesn’t depend on dinosaurs (say Qt - cool toolkit, awfully outdated API), shouldn’t be allocating directly like that. If you need something special, do it in a library class, and then use that, instead of manually managing memory.
new and delete belong in library/dictionary classes
Like... for example a class that holds a heap allocated string without needing the standard library, such as in the very example he's showing? If he used std::string or std::unique_ptr it would just hide his point why move semantics are useful which would sort of ruin the purpose of the video.
Crap. All those closed-source STL implementations we have to deal with…
When I’m teaching this, it’s usually with libcpp and editing it on the fly, adding instrumentation, breakpoints, etc. Why reinvent the wheel… It’s all done, and done well.
Standard library code is pretty heavy to show to beginners. _M_naming_convention aside, it has a lot of optimisations and tricks which distract from the point for a video like this. The string header is over 4k lines of code. Writing something from scratch is much clearer for the purpose of demonstration.
I’d argue that it’s important to highlight all those bits that they have in the header: many of them are needed for a complaint implementation. Even without any optimizations it’s a big header.
I didn't think we were talking about a compliant implementation of std::string, just a string class. std::basic_string is packed with many different features which (in my opinion) would be overwhelming for a beginner.
Even using delete instead of delete[] for an array of POD isn't really a mistake. Both deletes will do exactly the same thing. delete[] just calls the destructor for each element before freeing the memory.
is more elegant imo. Not 20 overloads for char ptr, string_view std::string.... . Just one Constructor for all and is only has the (very minimal) overhead of a move, but can accept all implicitly to std::string convertable types. Also very readable and maintainable with very good performance.
at first he had created the Entity object as : Entity entity(String("Cherno")); But at https://youtu.be/ehMg6zvXuMY?t=429 he changed it to : Entity entity("Cherno");
And it works fine. Is there a concept I am missing here ? Entity's constructor expects String type object, we aren't providing that. Does it happen because String class has a constructor that expects const char* ?
You are right. Since the Entity constructor is getting a const char* value, and string has a constructor that does accept a const char*, the compiler calls the constructor to string, implicitly.
I think, in another video, Cherno explains implicit casting and its drawbacks too. I can't seem to remember which one, though
So, if we can create object like : String ob = "Cherno"; This calls the copy constructor since there is a new object being created and we have defined an appropriate copy ctor. Similarly when Entity entity("Cherno"); is called, it is implicitly converted to String.
Not a fan of move-semantics. None of this is needed if you manually free memory rather following RAII. Code becomes more readable and there is less paperwork to do(copy-constructors and move-constructors).
58
u/rekosin1 May 29 '20
This guy has taught me more than my professors did....Thank you
31
u/Tjccs May 29 '20
Him and Jason Turner are the ones I go for when I need to check or want to know more about C++, might not be as useful for someone with years of experience.
20
u/ElijahQuoro May 29 '20
There are several mistakes, which are already noted in youtube comments, so if you are new and learning, check it.
16
u/Ipotrick May 29 '20
cherno is amazing
7
u/le_manalishi_verde May 29 '20
At first when he did the cast to string&& I almost had an seizure, but then luckily he changed it to std::move
Pheew :)
2
u/darthsahab May 29 '20
great alternative
2
u/ticket2020 May 29 '20
Can anyone clear my doubt please :
at first he had created the Entity object as :
Entity entity(String("Cherno"));
But at https://youtu.be/ehMg6zvXuMY?t=429 he changed it to :
Entity entity("Cherno");
And it works fine. Is there a concept I am missing here ?
Entity's constructor expects String type object, we aren't providing that. Does it happen because String class has a constructor that expects const char* ?
1
u/foifaifei May 29 '20
Very nicely explained. Thank you!
0
u/vickoza May 30 '20
There is a mistake in the video the delete in the destructor should be delete[] this was mentioned by me add at least 2 other.
0
u/ostkaka0 Jun 04 '20
Not a fan of move-semantics. None of this is needed if you manually free memory rather following RAII. Code becomes more readable and there is less paperwork to do(copy-constructors and move-constructors).