R
Robert
Hello all...
In my code below, the Notify Constructor and Destructor is getting
called twice and it appears that a new Notify object is created on the
2nd call. The 2nd call is caused by this line below: pNotify = new
EMAILnotify; that lives in the Notification Constructor.
One theory is that the Notify base class is not completely constructed
prior to using it in the Notification Constructor code: pNotify = new
EMAILnotify;
All the code below can be pasted into a *.cpp module and will compile
and run with a VC++6.0 compiler.
Thanks in advance for any clues as to why?
**************************************************************
#include <iostream>
#include <string>
using namespace std;
class Notify
{
public:
Notify(void){ cout << "Constructing Notify: " << this << endl; }
virtual ~Notify(void){ cout << "Destructing Notify: " << this <<
endl; }
virtual void Send() = 0;
};
class Notification : public Notify
{
public:
Notification(void){}
Notification(string sNotifyType);
virtual ~Notification(void);
void Send(){ pNotify->Send(); }
private:
Notify* pNotify;
};
class EMAILnotify : public Notify
{
public:
EMAILnotify(void){}
virtual ~EMAILnotify(void){};
virtual void Send(){ cout << "Send() Email: " << this << endl; }
};
Notification::Notification(string sNotifyType)
{
if(sNotifyType == "EMAIL")
pNotify = new EMAILnotify; // Causes 2nd Constructor call
}
Notification::~Notification(void)
{
if( NULL != pNotify )
delete pNotify;
}
void main()
{
Notification* pNotification = new Notification("EMAIL");
pNotification->Send();
delete pNotification;
}
In my code below, the Notify Constructor and Destructor is getting
called twice and it appears that a new Notify object is created on the
2nd call. The 2nd call is caused by this line below: pNotify = new
EMAILnotify; that lives in the Notification Constructor.
One theory is that the Notify base class is not completely constructed
prior to using it in the Notification Constructor code: pNotify = new
EMAILnotify;
All the code below can be pasted into a *.cpp module and will compile
and run with a VC++6.0 compiler.
Thanks in advance for any clues as to why?
**************************************************************
#include <iostream>
#include <string>
using namespace std;
class Notify
{
public:
Notify(void){ cout << "Constructing Notify: " << this << endl; }
virtual ~Notify(void){ cout << "Destructing Notify: " << this <<
endl; }
virtual void Send() = 0;
};
class Notification : public Notify
{
public:
Notification(void){}
Notification(string sNotifyType);
virtual ~Notification(void);
void Send(){ pNotify->Send(); }
private:
Notify* pNotify;
};
class EMAILnotify : public Notify
{
public:
EMAILnotify(void){}
virtual ~EMAILnotify(void){};
virtual void Send(){ cout << "Send() Email: " << this << endl; }
};
Notification::Notification(string sNotifyType)
{
if(sNotifyType == "EMAIL")
pNotify = new EMAILnotify; // Causes 2nd Constructor call
}
Notification::~Notification(void)
{
if( NULL != pNotify )
delete pNotify;
}
void main()
{
Notification* pNotification = new Notification("EMAIL");
pNotification->Send();
delete pNotification;
}