A Question Regarding My Inheritance Code

CNPS

Distinguished
Jul 4, 2011
1
0
18,510
0
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
3
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.
 
Thread starter Similar threads Forum Replies Date
M Apps General Discussion 1
S Apps General Discussion 1
P Apps General Discussion 0
T Apps General Discussion 10
K Apps General Discussion 0
T Apps General Discussion 2
H Apps General Discussion 1
P Apps General Discussion 0
B Apps General Discussion 2
L Apps General Discussion 1
N Apps General Discussion 5
T Apps General Discussion 1
N Apps General Discussion 3
M Apps General Discussion 3
P Apps General Discussion 2
appletatoes Apps General Discussion 3
M Apps General Discussion 1
B Apps General Discussion 7
tjones9163 Apps General Discussion 2
S Apps General Discussion 3

Similar threads


ASK THE COMMUNITY