Calling Dtor at the End of a Class Method
Hi,
I have a class hierarchy as follows:
class InternalRow {
public:
virtual int get() const = 0;
virtual void set() = 0;
};
class PhysicalRow: public InternalRow {
public:
int get() const;
void set();
};
class VirtualRow: public InternalRow {
public:
int get() const;
void set();
};
class Row: public InternalRow {
public:
int get();
void set();
private:
InternalRow *row;
};
User is interfaced to the rows through Row class. And Row redirects
get/set calls to the related InternalRow implementation pointed by
Row::row field. There are two InternalRow implementations: 1)
PhysicalRow, which holds actual data, 2) VirtualRow, which holds a
reference to some PysicalRow. VirtualRow redirects get methods asis to
the related PhysicalRow. But when a set() call is made on a
ReferenceRow, I'd like that ReferenceRow to get upgraded to a
PhysicalRow. For this purpose, I replaced "void set()" methods with
"InternalRow* set()" methods, and call set() methods in Row as "row =
row->set()". To summarize, new design becomes something like this:
class InternalRow {
public:
virtual int get() const = 0;
virtual InternalRow* set() = 0;
};
class PhysicalRow: public InternalRow {
public:
int get() const;
InternalRow* set();
};
class VirtualRow: public InternalRow {
public:
int get() const;
InternalRow* set() {
return (new PhysicalRow())->set();
}
};
class Row: public InternalRow {
public:
int get();
void set() { row = row->set(); }
private:
InternalRow *row;
};
But here, the caller ReferenceRow instance should be free'd just after
returning (new PhysicalRow())->set(). How can I call the dtor at the
end of ReferenceRow::set() calls? Do you suggest a different approach?
What are your ideas?
Best.