Skip to main content
added 49 characters in body; deleted 2 characters in body; deleted 1 characters in body; deleted 11 characters in body
Source Link
Steve Jessop
  • 280.7k
  • 40
  • 473
  • 709

Encapsulate each resource in a class which clears them in its destructor (with a surrounding try/catch):

struct ProperlyManagedA { // some means of using the resource - a rudimentary way is this: A &getA() { return a; } const A &getA() const { return a; } // cleanup ~ProperlyManagedA() { try { a.clear(); // whatever it is ClearResourceA actually does } catch (...) {} } private: A a; } 

A shared_ptr with a custom deleter is one way to achieve this without having to create a whole class for each type of resource.

You might be able to better thanimprove on discarding the exception (log the problem, for example), depending what's thrown.

Even better, modify resources A, B and C so that they clear themselves in their own destructors. That might not be possible, though.

Either way, you can then put as many such resources in a single class as you like, without adding any code to the destructor of the class. This is "scalability". The whole point of RAII, is that each user of a resource shouldn't have to write cleanup code in order to use the resource correctly.

Encapsulate each resource in a class which clears them in its destructor (with a surrounding try/catch):

struct ProperlyManagedA { // some means of using the resource - a rudimentary way is this: A &getA() { return a; } const A &getA() const { return a; } // cleanup ~ProperlyManagedA() { try { a.clear(); } catch (...) {} } private: A a; } 

A shared_ptr with a custom deleter is one way to achieve this without having to create a whole class for each type of resource.

You might be able to better than discarding the exception (log the problem, for example), depending what's thrown.

Even better, modify resources A, B and C so that they clear themselves in their own destructors. That might not be possible, though.

Either way, you can then put as many such resources in a single class as you like, without adding any code to the destructor of the class. This is "scalability". The whole point of RAII, is that each user of a resource shouldn't have to write cleanup code in order to use the resource correctly.

Encapsulate each resource in a class which clears them in its destructor (with a surrounding try/catch):

struct ProperlyManagedA { // some means of using the resource - a rudimentary way is this: A &getA() { return a; } const A &getA() const { return a; } // cleanup ~ProperlyManagedA() { try { a.clear(); // whatever it is ClearResourceA actually does } catch (...) {} } private: A a; } 

A shared_ptr with a custom deleter is one way to achieve this without having to create a whole class for each type of resource.

You might improve on discarding the exception (log the problem, for example), depending what's thrown.

Even better, modify resources A, B and C so that they clear themselves in their own destructors. That might not be possible, though.

Either way, you can then put as many such resources in a single class as you like, without adding any code to the destructor of the class. This is "scalability". The whole point of RAII, is that each user of a resource shouldn't have to write cleanup code in order to use the resource correctly.

Source Link
Steve Jessop
  • 280.7k
  • 40
  • 473
  • 709

Encapsulate each resource in a class which clears them in its destructor (with a surrounding try/catch):

struct ProperlyManagedA { // some means of using the resource - a rudimentary way is this: A &getA() { return a; } const A &getA() const { return a; } // cleanup ~ProperlyManagedA() { try { a.clear(); } catch (...) {} } private: A a; } 

A shared_ptr with a custom deleter is one way to achieve this without having to create a whole class for each type of resource.

You might be able to better than discarding the exception (log the problem, for example), depending what's thrown.

Even better, modify resources A, B and C so that they clear themselves in their own destructors. That might not be possible, though.

Either way, you can then put as many such resources in a single class as you like, without adding any code to the destructor of the class. This is "scalability". The whole point of RAII, is that each user of a resource shouldn't have to write cleanup code in order to use the resource correctly.