On 10 Dec 2003 10:20:24 -0800,
(Robert) wrote:
>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.
Class Notification derives from Notify. The calling sequence is:
(1) create a Notification object, which executes the Notify constructor,
and then (2) executes the Notification constructor body, which (3)
executes pNotify = new EMAILnotify, which (4) creates an EMAILnotify
object, where EMAILnotify is derived from Notify, so that this creation
(5) executes the Notify constructor for the EMAILnotify object, and (6)
proceeds to execute the EMAILnotify constructor body.
>...
>
>************************************************* *************
>#include <iostream>
>#include <string>
>
>using namespace std;
>
>class Notify
This is a bad name for a class. A class doesn't "do" anything,
unless it's a functor class. Which this isn't.
>{
>public:
> Notify(void){ cout << "Constructing Notify: " << this << endl; }
Style.
'void' as an argument list is a C'ism which you'd better forget.
> virtual ~Notify(void){ cout << "Destructing Notify: " << this <<
>endl; }
> virtual void Send() = 0;
>};
Style.
At least one empty line between class declarations is a good idea.
>class Notification : public Notify
>{
>public:
> Notification(void){}
> Notification(string sNotifyType);
> virtual ~Notification(void);
> void Send(){ pNotify->Send(); }
>private:
> Notify* pNotify;
>
>};
Style.
The definitions of the Notification member functions should appear
here, before anything more. One "place" per class in the code.
Design.
Having a pNotify member doesn't seem to serve any useful purpose.
Most probably you have tried to implement too much in one class.
>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")
Design.
Don't identify types by strings (in general). Use symbolic constants
if you absolutely have to. But even more to the point: don't identify
types by data unless you really Really REALLY have to.
> pNotify = new EMAILnotify; // Causes 2nd Constructor call
>}
>Notification::~Notification(void)
>{
> if( NULL != pNotify )
Coding.
No need to check for NULL.
> delete pNotify;
>}
>
>void main()
C++ standard.
'void main' is not allowed. This is not a valid standard C++ program.
>{
> Notification* pNotification = new Notification("EMAIL");
> pNotification->Send();
> delete pNotification;
Style.
The above is not exception safe in any sense. Use a std::auto_ptr.
>}