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.
[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.