// Note: This is C, not C++.
struct Base
{
int *Member; // Cannot initialize this because constructors don't exist.
};
// Here's a method that initializes the given Base instance.
bool InitializeBase( struct Base *ThisBase )
{
if( ThisBase == NULL )
{
returnfalse;
}
// Initialize Member. This is potential memory leak.
if( ( ThisBase -> Member = ( ( int * )malloc( sizeof( int ) ) ) == NULL )
{
returnfalse;
}
returntrue;
}
How would I know if Member of Base is already allocating memory or pointing to another object? Should I initialize Member after each instance of Base is made?
I would probably go with something like the factory pattern (have a function that returns objects) to prevent users from making object but not initializing it. Destructing it is still up them (in C anyway).