C++ RAII vs Rust OBRM - Part 1

preview_player
Показать описание
Today we will be discussing the RAII pattern in C++.

📝 Get your FREE Rust cheat sheet :

#rust #c++ #raii
Рекомендации по теме
Комментарии
Автор

3:15 a little correction: In the case of std::ofstream, when it's destructor is called, the file will be closed (it is closed by the filebuf, which represents the internal stream buffer). In other words, std::ofstream manages the acquired file.

Nuclear
Автор

10:09 destructors are actually called in the reverse order that the objects have been constructed. This is also true for members of a class and even globals. It just makes sense, as you could imagine passing a reference to a previous object to a new object. If this new object's destruction needs to do some stuff on the previous object, we can be sure, that it lives long enough.

Possseidon
Автор

Nice video but the C++ example showing how a file handle would not be closed if an exception is thrown is incorrect. ofstream is a RAII wrapper around a “raw file handle”, meaning the file handle would actually get closed as the ofstream instance falls out of scope (including if the exception is thrown).

brainplot
Автор

I come from C so anything related to C++ has always been very confusing to me. This video really helps me getting its concepts. :D

Speykious
Автор

i got some doubts!!
the "variable" car is a object or instance of "class" CarManager and is not a pointer to "class" Car. so you can't directly access anything from "class" Car. you can indirectly do it by "car.p", since it is a pointer to "class" Car, wait a second but "p" is a private member so you can't acess it. hence technically no way to aceess "class" Car, it's method, and it's members.

In case of make_unique and make_shared, they give you a pointer to "class" to Car, so you can access it's members by car->member or (*car).member .

so i think the CarManager implementation is not appropriate.
C++ guys, correct me, if i am mistaken.

harikrishnanb
Автор

Thanks for the excellent introduction to RAII! As an experienced programmer who has never worked with C++, I had a basic understanding of what RAII means to that community, but your examples were very helpful! I'm looking forward to your discussion of OBRM in Rust!

philstubblefield
Автор

I can tell the channel is taking off because my boy is looking more and more dapper

HylianEvil
Автор

This is by far the most clear explanation of RAII I've seen so far. Thank you!

philipreinhold
Автор

Wow this was great, you need to make more comparison videos with c++ and possibly other popular languages with rust!

johaneriksson
Автор

A Rust channel with a nice C++ video. Keep up the good work.

pdd
Автор

At 10:10 you say car is cleaned up and then car2. That's the other way around. Whatever is created first is destroyed last. This makes much more sense, because you can add an object ref to another object and if you were to destroy it first, it would orphan that reference, causing a segv.
It's first in, last out.

nextlifeonearth
Автор

I've learned C++ from this video. You are an amazing C++ teacher. Thanks a lot.

kcvinu
Автор

I think the CarManager is not a good example of RAII - it cleans up memory that it does not itself allocate, but that gets passed in. There is nothing preventing be from passing the same pointer to two CarManagers, which will lead to a double free.

alagaika
Автор

Nice, your content quality is better and better!

babatona
Автор

one key to note that php uses this technique of reference counters for variables and the garbage collector will frequently check to free memory that counts by zero

marawanlotfy
Автор

By the way, I believe the name Recourse Acquisition Is Initialization comes from the object being fully initialized in the constructor code so that any object that is acquired through construction is already initialized and can be used. The alternative is that initialization code is not put in the constructor but in a separate Initialize() function. The user/caller is expected to construct the object and then immediately call Initialize() to initialize it, and the object is considered not valid and should not be used until Initialize() is called. The motivation for doing the two step process is that constructors cannot have a return value and so cannot report if the initialization code failed in some way. To put the initialization code in the constructor would require exception handling which some people prefer not to use. However initializing in a separate function leaves the possibility that a user could accidentally try to use an uninitialized object. So the technique of RAII ensures that all successfully acquired objects are also initialized.

jimhewes
Автор

@3:08 You do not need to explicitly close fstream in C++. The fstream object will be closed automatically when it goes out of scope.

apivovarov
Автор

Clean explanations. Even for non english speakers. Greetings from Brazil!!

putzz
Автор

You’re amazing in explanation bro, thanks

Khalooodi-hewe
Автор

love you buddy thank you so much for the valuable knowledge

PHANTOMWORLD