A Question Regarding My Inheritance Code

CNPS

Distinguished
Jul 4, 2011
1
0
18,510
Firstly, here's mode code:
[cpp]
struct Base
{
Base( void ) : Member( NULL ) { }
~Base( void )
{
this->Release( );
}

protected:
int *Member;

const int &Get( void ) const
{
return *this->Member;
}

const bool Set( const int New )
{
if( !this->Member )
{
this->Member = ( new( std::nothrow ) int( New ) );
if( !this->Member )
return false;

else return true;
}

else
{
*this->Member = New;
return true;
}
}

void Release( void )
{
delete this->Member;
this->Member = NULL;
}
};

struct Main : protected Base
{
const int &GetBaseMember( void )
{
return this->Get( );
}

const int SetBaseMember( const int New )
{
if( !this->Set( New ) )
return -1;

else
return GetBaseMember( );
}
};
[/cpp]

Here's my question: Notice how Base allocates resources when Base::Set( ) is called. Now, since Main derives from Base, will the destructor of Base be called (releasing the resources allocated by the instance) when Mains destructor is called?

Note: I'm new to inheritance.

Any help is appreciated.
 

theDanijel

Distinguished
May 4, 2011
74
0
18,590
first of all, how can you inherit structs? Instead of struct I think the keyword is class!
Guessing this is C++, the resources allocated will not be released in the destructor, but only if you manualy call the Release method.
The default destructor will be called for base class, but will not release the resources allocated.