M
Matan Nassau
This question is a little embarrassing... I have a boolean expressions
interpreter with its composite (see Go4's "Design Patterns" book).
here is a simple leaf of the composite:
class Constant : public BooleanExp {
public:
Constant(bool);
virtual ~Constant();
virtual void Accept(BooleanExpVisitor&);
bool GetValue() const {return _value;}
private:
bool _value;
};
and here is a composite complying with the BooleanExp interface:
class AndExp : public BooleanExp {
public:
AndExp(BooleanExp*, BooleanExp*);
virtual ~AndExp();
virtual void Accept(BooleanExpVisitor&);
private:
BooleanExp* _operand1;
BooleanExp* _operand2;
};
Consider a few other classes that are similar in concept, including
OrExp, VariableExp and so on. The Context class maps variables to
their boolean values.
Now I want to evaluate an expression:
BooleanExp *expression;
Context cont;
context.Assign(x, false);
context.Assign(y, true);
EvaluationVisitor visitor(cont);
expression = new OrExp (
new AndExp (new Constant (true), x),
new AndExp (y, new NotExp (x))
);
bool result = expression->Accept(visitor);
Up to here everything is good. My question is, how do I collect the
garbage?
What I have at the moment is
delete expression;
Do you think this will do?
I have a virtual destructor for each of the BooleanExp (concrete)
subclasses, and they all do nothing. Correct me if I'm wrong, but I
don't think it's a good idea to let these destructors have the
responsibility to clean up since the dynamic allocations are performed
out of the class.
TIA,
Matan Nassau
interpreter with its composite (see Go4's "Design Patterns" book).
here is a simple leaf of the composite:
class Constant : public BooleanExp {
public:
Constant(bool);
virtual ~Constant();
virtual void Accept(BooleanExpVisitor&);
bool GetValue() const {return _value;}
private:
bool _value;
};
and here is a composite complying with the BooleanExp interface:
class AndExp : public BooleanExp {
public:
AndExp(BooleanExp*, BooleanExp*);
virtual ~AndExp();
virtual void Accept(BooleanExpVisitor&);
private:
BooleanExp* _operand1;
BooleanExp* _operand2;
};
Consider a few other classes that are similar in concept, including
OrExp, VariableExp and so on. The Context class maps variables to
their boolean values.
Now I want to evaluate an expression:
BooleanExp *expression;
Context cont;
context.Assign(x, false);
context.Assign(y, true);
EvaluationVisitor visitor(cont);
expression = new OrExp (
new AndExp (new Constant (true), x),
new AndExp (y, new NotExp (x))
);
bool result = expression->Accept(visitor);
Up to here everything is good. My question is, how do I collect the
garbage?
What I have at the moment is
delete expression;
Do you think this will do?
I have a virtual destructor for each of the BooleanExp (concrete)
subclasses, and they all do nothing. Correct me if I'm wrong, but I
don't think it's a good idea to let these destructors have the
responsibility to clean up since the dynamic allocations are performed
out of the class.
TIA,
Matan Nassau