Is there a way to use Object.factory with templated classes? Or some way to construct templated classes given RTTI of an instance?
Steven Schveighoffer
schveiguy at gmail.com
Wed Sep 26 21:25:07 UTC 2018
On 9/26/18 4:41 PM, Chad Joan wrote:
> Hi all,
>
> I'm implementing a deep-copy method for a tree of templated class
> instances. As part of this, I need some way to copy each node. I want
> to avoid code that does things like casting objects into byte arrays and
> then copying raw bytes; I want all operations to be memory safe things
> that I can use at compile-time. So I planned to make all of these have
> default constructors and use Object.factory to at least create the
> correct instance type at the destination. The classes can implement
> auxiliary copy methods if they need to copy anything that isn't already
> handled by their deepCopy method.
>
> But I ran into a problem: Object.factory doesn't seem to be compatible
> with templated classes.
Object.factory is a really old poorly supported type of reflection. I
would not depend on it for anything.
You are better off using your own registration system.
As far as choosing the design for your problem, you can use:
auto obj = typeid(obj).create();
which is going to work better, and doesn't require a linear search
through all modules/classes like Object.factory.
If it were me, I'd probably instead implement a clone virtual method.
This way if any custom things need to occur when copying the data, it
can be handled.
-Steve
More information about the Digitalmars-d-learn
mailing list