Re: Calling Dtor at the End of a Class Method
On Apr 21, 10:20 pm, Volkan YAZICI <volkan.yaz...@gmail.com> wrote:
[... snip totally confusing code ...]
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()".
[... snip totally confusing code ...]
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?
Your reference row or virtual row (or what it was?) looks like
shared_ptr<PhysicalRow>. What you seemingly want to achieve is copy-on-
write. That is however most easy to achieve with pimpl idiom. Also you
add inheritance for some unknown reason. So your design seems to
result with confused mix of these three things and numerous rather
empty classes that do nothing but forward each other endlessly. Start
by implementing one thing that looks like a class:
// Row.h
class Row
{
public:
Row();
~Row();
Row(Row const&);
Row& operator=(Row const&);
int get() const;
void set();
private:
class InternalRow;
InternalRow* pimpl;
};
That is the only thing that user will see. Rest of your logic hide
into Row.cpp.
If you need to refcount, refcount intrusively in IntrnalRow, if you
need to copy on write in set() then copy object behind pimpl and
assign copy to pimpl. If you need whole cascade of InternalRow-derived
classes then go ahead (inside Row.cpp), but don't derive class Row
from it.