2 added 272 characters in body
source | link

The benefit to RAII is ultimately encapsulation, in that once you've created an object you no longer need to worry about what to do with it once you've finished using it.

So, if you have a class that opens a file handle, you can make whatever operations you like on that object (which affects the underlying file) and when you destroy the object, you no longer have to think "do I have to close the file handle" or "is the file handle closed yet". Its gone, dealt with, fully automatically handled for you. And that magic happens because of RAII.

So you can compare RAII to C-style manual memory management, where you'd open a file, pass the handle around to various functions, and then have to remember to pass the handle to a close_file() function. You can compare it to 'coffee'-style garbage collection manual object management, where you'd create an object that opens a file, perform operations on the object and then have to remember to call dispose or wrap your object in a using block to make the dispose method run - and have to put special code in your finaliser to clear it up as well.

And then you can show the C++ way where you don't have to remember to do anything at all, it just magically works for you, right there and then (so if you suddenly have to re-open the file, it's not locked by an object that is waiting to be garbage collected).

PS. Herb Sutter's talk at Build about modern C++ features has a section on RAII, starting at 13:50 minutes in where he shows a comparison between C-style code and C++ RAII code.

The benefit to RAII is ultimately encapsulation, in that once you've created an object you no longer need to worry about what to do with it once you've finished using it.

So, if you have a class that opens a file handle, you can make whatever operations you like on that object (which affects the underlying file) and when you destroy the object, you no longer have to think "do I have to close the file handle" or "is the file handle closed yet". Its gone, dealt with, fully automatically handled for you. And that magic happens because of RAII.

So you can compare RAII to C-style manual memory management, where you'd open a file, pass the handle around to various functions, and then have to remember to pass the handle to a close_file() function. You can compare it to 'coffee'-style garbage collection manual object management, where you'd create an object that opens a file, perform operations on the object and then have to remember to call dispose or wrap your object in a using block to make the dispose method run - and have to put special code in your finaliser to clear it up as well.

And then you can show the C++ way where you don't have to remember to do anything at all, it just magically works for you, right there and then (so if you suddenly have to re-open the file, it's not locked by an object that is waiting to be garbage collected).

The benefit to RAII is ultimately encapsulation, in that once you've created an object you no longer need to worry about what to do with it once you've finished using it.

So, if you have a class that opens a file handle, you can make whatever operations you like on that object (which affects the underlying file) and when you destroy the object, you no longer have to think "do I have to close the file handle" or "is the file handle closed yet". Its gone, dealt with, fully automatically handled for you. And that magic happens because of RAII.

So you can compare RAII to C-style manual memory management, where you'd open a file, pass the handle around to various functions, and then have to remember to pass the handle to a close_file() function. You can compare it to 'coffee'-style garbage collection manual object management, where you'd create an object that opens a file, perform operations on the object and then have to remember to call dispose or wrap your object in a using block to make the dispose method run - and have to put special code in your finaliser to clear it up as well.

And then you can show the C++ way where you don't have to remember to do anything at all, it just magically works for you, right there and then (so if you suddenly have to re-open the file, it's not locked by an object that is waiting to be garbage collected).

PS. Herb Sutter's talk at Build about modern C++ features has a section on RAII, starting at 13:50 minutes in where he shows a comparison between C-style code and C++ RAII code.

1
source | link

The benefit to RAII is ultimately encapsulation, in that once you've created an object you no longer need to worry about what to do with it once you've finished using it.

So, if you have a class that opens a file handle, you can make whatever operations you like on that object (which affects the underlying file) and when you destroy the object, you no longer have to think "do I have to close the file handle" or "is the file handle closed yet". Its gone, dealt with, fully automatically handled for you. And that magic happens because of RAII.

So you can compare RAII to C-style manual memory management, where you'd open a file, pass the handle around to various functions, and then have to remember to pass the handle to a close_file() function. You can compare it to 'coffee'-style garbage collection manual object management, where you'd create an object that opens a file, perform operations on the object and then have to remember to call dispose or wrap your object in a using block to make the dispose method run - and have to put special code in your finaliser to clear it up as well.

And then you can show the C++ way where you don't have to remember to do anything at all, it just magically works for you, right there and then (so if you suddenly have to re-open the file, it's not locked by an object that is waiting to be garbage collected).