Hello ladies and gents,

Got a question, is it wise to create a member of a class by using another class it's constructor and deleting it threw the use of that class it destructor?

For example:

// Testing code.

#include "first.h"

int main()
{
	first myMenu;

	bool gameLoop = true;

	while (gameLoop)
	{
		gameLoop = false;
	}

    return 0;
}
#include "second.h"

class first
{
public:
	first();
	~first();

private:
	second *mySecond;
};
#include "first.h"

first::first()
{
	mySecond = new second;
}

first::~first()
{
	delete mySecond;
}
class second
{
public:
	second();
	~second();
};
#include "second.h"

second::second() {}

second::~second() {}

Is this something that is done or should it be avoided?

Thank you.

a. if second is not an object oriented type, using value semantics would be simpler:

#include "second.h"
class first
{
    private:
        second mySecond;
};

b. if pointer semantics are required, you would have to decide: is the pointer an *owning* pointer? if is is, what would happen if an object of type first is copied? one option is to disable copy and assignment.

#include "second.h"
class first
{
    public:
        first() : mySecond( new second ) {}
        ~first() { delete mySecond ; }
    private:
        second *mySecond;
        // disable copy and assignment; do not define these!
        first( const first& ) ;
        void operator= ( const first& ) ;
};

another would be to use the semantics of transfer of ownership on copy or assignment

#include "second.h"
 class first
 {
    public:
        first() : mySecond( new second ) {}
    private:
        std::auto_ptr<second> mySecond;
};

in c++0x, you could also implement shared ownership easily. (in c++98, you could use boost::shared_ptr<>for this)

#include "second.h"
class first
{
    public:
        first() : mySecond( new second ) {}
    private:
        std::shared_ptr<second> mySecond; // c++0x
};

What you've gotta remember is that there you're going to have to delete the pointer to second in first's member destructer - unless I've misunderstood whenever you use new you must use delete. So because second is allocated in first (dynamically), you do indeed need to delete it in first. That's not to be mistaken with destructing second which is managed by second's own destructor. Try this:

class first {
private:

public:
  first( void ) {
  }

  ~first( void ) {
    std::cout<< "First's destructor!\n";
  }
};

class second {
private:
  first *fir;

public:
  second( void )
    : fir( new first ) {
  }
  ~second ( void ) {
    std::cout<< "Second's destructor!\n";
    delete fir;
  }
};

int main( void ) {
  second sec;
}

which tells you that both first and second's destructors are both called! That make sense?

Thank you for the explanation and examples gentlemen :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.