Re: Destructor question
"Duane Hebert" wrote:
Actually, each 'new' wraps the previous object, and so the one delete at
the
end invokes that wrappers destructor which in turn deletes the object it
wraps through the magic of virtual destructors.
All works fine now, with the proper object destruction!
huh?
Ok, in main() we have:
Beverage* pBeverage = new Darkroast();
pBeverage = new Mocha(pBeverage);
pBeverage = new Mocha(pBeverage);
pBeverage = new Whip(pBeverage);
cout << pBeverage->getDescription() << " $" << pBeverage->cost() << endl;
delete pBeverage; pBeverage = NULL;
This was coded to demonstrate the 'decorator' pattern in which we add new
behavior to a base class without resorting to inherritance. By avoiding
inheritance we better encapsulate the code which is adding costs depending on
what extras a customer adds to their lotte. If stuff were 'hard coded' in
the base class Beverage, then any time that stuff changed (ie a new topping
was needed) we'd have to recompile. With decorators, our original code would
be closed to modification, but open to extensions.
How? Well, we create an abstract decorator class ( CondimentDecorator )
which is derived from Beverage. We then derived the concrete classes Mocha,
Soy, & Whip from it. As they are also Beverages, this allows us to use them
where we would any Beverage. In them, we have a private Beverage* pBeverage
that allows us to 'wrap' the original concrete object in the decorator's
constructor.
So, for example:
// This instantiate a Darkroast, dynamically at run time
// assigning it to a Beverage pointer using polymorphism
// vs. Beverage beverage = new Darkroast();
Beverage* pBeverage = new Darkroast();
// Now, this pointer is passed to the decorator's constructor
// and, of course, this passes a new beverage point back
pBeverage = new Mocha(pBeverage);
The Mocha class looks like this:
#include "Beverage.h"
#include "CondimentDecorator.h"
using namespace std;
class Mocha : public CondimentDecorator
{
public:
Mocha();
Mocha(Beverage*);
~Mocha(void);
string getDescription();
double cost();
private:
Beverage* pBeverage;
};
and if you look at the destructor in Mocha.cpp it looks like this:
Mocha::~Mocha(void)
{
delete this->pBeverage; // This was set in the constructor
}
So, when it's pointer is destroyed, it delete's the wrapped pointer.
If you'd like the code, send me an email and I'll send you a zip file for
you to look at.
Patrick
Benchmark Studios
pwaugh@comcast dot net