[Greenengine-developer] a note on dynamic casts of shared_ptr
Status: Planning
Brought to you by:
jeromiya
|
From: Jeremy B. <bel...@gm...> - 2007-04-25 23:37:20
|
Given the following classes:
class base {
public:
virtual ~base() {}
};
class sub : public base {
public:
virtual ~sub() {}
};
The following does NOT work:
int main()
{
shared_ptr<base> b(new base()); // BAD
shared_ptr<sub> a(dynamic_pointer_cast<sub>(b));
assert(a.get()); // ASSERT WILL FAIL
return 0;
}
The following WILL work:
int main()
{
shared_ptr<base> b(new sub()); // GOOD
shared_ptr<sub> a(dynamic_pointer_cast<sub>(b));
assert(a.get()); // ASSERT WILL PASS
return 0;
}
It basically means I have to essentially rewrite any subclass functions
that instantiate objects, like TemplateTable::Instantiate all need to be
rewritten in the subclasses to make a new instance of the subclass they
work with, rather than the base class. There may be other places where I
should do this, I'll have to see.
Regards,
Jeremy
|