#include <iostream>
class Foo {
public:
Foo(int ival, double dval) : x(ival), y(dval) {};
~Foo();
private:
int x;
double y;
};
inline
Foo::~Foo()
{
x = 0;
y = 0.0;
}
int main() {
return 0;
}
// unnecessary -- carried out implicitly by compiler
if ( ptr != 0 ) delete ptr;
Explicit destructor invocation often in conjunction with replacement new. For example:
#include <iostream>
#include <new>
class Image {
public:
Image(const std::string& name) : _name (name)
{
std::cout << "Called constructor" << std::endl;
}
~Image(){
_name.clear();
std::cout << "Called destructor" << std::endl;
}
private:
std::string _name;
};
int main() {
// allocate a heap buffer of size sizeof(Image) bytes.
std::cout << "Step1:" << std::endl;
char *arena = new char [sizeof(Image)];
// construct Image in pre-allocated memory
std::cout << "Step2:" << std::endl;
Image *ptr = new (arena) Image("Qhsil"); // Called constructor
// manually destroy
std::cout << "Step3:" << std::endl;
ptr->~Image(); // Called destructor
// construct Image in pre-allocated memory
std::cout << "Step4:" << std::endl;
ptr = new (arena) Image("Dqqkd"); // Called constructor
// manually destroy
std::cout << "Step5:" << std::endl;
ptr->~Image(); // Called destructor
// free allocated memory
std::cout << "Step6:" << std::endl;
delete[] arena;
std::cout << "Finished." << std::endl;
return 0;
}
#include <iostream>
#include <new>
#include <string>
class Image {
public:
Image(const std::string& name, int index) : _name(name), _index(index) {}
~Image() {
_name.clear();
_index = -1;
}
private:
std::string _name;
int _index;
};
int main() {
int numImgs = 10;
char* arena = new char[sizeof(Image) * numImgs];
Image* images[numImgs];
// 1st group of images
for (int i = 0; i < numImgs; ++i) {
char* ptr = arena + i * sizeof(Image);
images[i] = new (ptr) Image("Qhsil", i);
}
for (int i = 0; i < numImgs; ++i) {
images[i]->~Image();
}
// 2nd group of images
for (int i = 0; i < numImgs; ++i) {
char* ptr = arena + i * sizeof(Image);
images[i] = new (ptr) Image("Dqqkd", i);
}
for (int i = 0; i < numImgs; ++i) {
images[i]->~Image();
}
delete[] arena;
return 0;
}
However, using std::vector is safer and more flexible.
Account acct( "Tina Lee" );
int swt;
// ...
switch( swt ) {
case 0:
return;
case 1:
// do something
return;
case 2:
// do something else
return;
// and so on
}
the solution is either to declare the destructor to be non-inline or to rewrite the program code as follows:
// rewritten to provide a single return point
switch( swt ) {
case 0:
break;
case 1:
// do something
break;
case 2:
// do something else
break;
// and so on
}
// single return point
return;