0

I would like to create an Auto_Ptr to a legacy C struct; I only have header files and static libraries so I cannot modify the source code. I wondered wouldn't the auto_ptr automatically try to call the Destructor ~foo() if I call p.release() or p.reset()? This destructor does not exist for the C struct. My current solution is to write a C++ wrapper class that presents a destructor interface (internally it uses the library-specific C methods to release memory and destroy other resources). Is there a nicer way to do it; or has this problem already been solved by Boost etc.?

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
Matthias Hueser
  • 255
  • 1
  • 3
  • 11
  • Remember that in C++ the only difference between `struct` and `class` is that `members are by default `public` in a `struct` but `private` in a `class`. This means that you can have member functions in a `struct` like in a `class`, including constructors/destructors. And that the C++ compiler will add default constructors/destructors if you don't have any. – Some programmer dude Jul 27 '12 at 09:53
  • I was aware of that-- in my case C++ cannot possibly do the correct thing in the auto-generated destructor. I could edit the header-file and add functions but this wouldn't be a clean solution since for each new revision of the system header I would have to do that modification again. Does using a C++ wrapper sound like a good solution though? – Matthias Hueser Jul 27 '12 at 10:45

1 Answers1

2

There still exists an implicit destructor when compiled with c++, so its fine. If the struct is allocated with new, that is.

yuri kilochek
  • 12,709
  • 2
  • 32
  • 59