<<< struct Rectangle with dynamic memory allocation | Index | Huge problem discovered >>> |
A destructor is called for an object
when the object goes out of scope;
when the object containing it is destroyed.
struct Rectangle { int width; // member variable int height; // member variable char* ptr_name; //... Rectangle( char const* ptr_name_, int width_ , int height_ ) : width( width_ ), height( height_ ), ptr_name( nullptr ) { set_name( ptr_name_ ); } ~Rectangle() { if ( ptr_name ) delete[] ptr_name; ptr_name = nullptr; } void set_name( char const* ptr_name_ ) { if ( ptr_name ) delete[] ptr_name; int name_length = strlen( ptr_name_ ) + 1; ptr_name = new char[ name_length ]; strncpy( ptr_name, ptr_name_, name_length ); } //... };//struct Rectangle
<<< struct Rectangle with dynamic memory allocation | Index | Huge problem discovered >>> |