Retry: How to create an instance of a class by class name in a string?

Retry: How to create an instance of a class by class name in a string?

Post by Marc Youn » Sun, 19 Oct 2003 10:52:56



Sorry, newsreader went on the fritz.

Thanks for the responses!

The classes descend from TObject. I would like to stay away from
TPersistent because it adds memory to the class instances. I will be
loading thousands of objects into memory so access is very fast. I could
make my own class registration that stores the entry point to Create. That
would allow me to access the proper constructor by it's class name in a
string. That would also allow me to write plug-in dlls that have registered
types as well. I understand virtual and static methods from a usage
standpoint, an ancestor method that calls a virtual method will actually
call the correct descendant class' method. Since I will need to call the
correct create method manually does it matter if the constructors are
virtual or static?

Thanks.
 
 
 

Retry: How to create an instance of a class by class name in a string?

Post by Maarten Wi » Mon, 20 Oct 2003 01:12:03


I noticed. Please disregard the testy reply to the previous attempt.




It doesn't, really. It adds a whopping six methods and overrides
the inherited destructor, but it adds no fields. Even so, choosing
the right base class is much more important than a few bytes of
memory, even multiplied by ten thousand.



You mean the constructors aren't all the same? Then virtual constructors
wouldn't work for you, but you've defeated your own purpose anyway.

Doing your own class registration is absolutely possible; I've done it,
too. Just add your own RegisterSomething variant to the initialization
section of every unit that declares a new descendant.

Groetjes,
Maarten Wiltink

 
 
 

Retry: How to create an instance of a class by class name in a string?

Post by Rob Kenned » Mon, 20 Oct 2003 02:30:08


It adds absolutely no memory to the class instances. It adds exactly 12
bytes to the VMT and eight bytes to the DMT, which are both shared by
all instances of the class.


Knowing the address of the constructor does not allow you call the
constructor correctly. When you call a constructor, the compiler inserts
special code. You should store a class reference, not a method address.


So would RegisterClass and GetClass.


Why do you need to call the correct method manually?

If you're going to register other classes from plug-ins, then it is
impossible to know the exact class at compile time, so you won't be able
to call every constructor manually. You will need to have a virtual
constructor. You're making implementation decisions prematurely, and by
doing do, you're painting yourself into a corner.

--
Rob
 
 
 

Retry: How to create an instance of a class by class name in a string?

Post by Bruce Robe » Wed, 22 Oct 2003 01:42:07


That

As others have pointed out tPersistent adds no significant memory usage to
its instances. I suspect that if you coded your own registration process you
would add as much overhead. If speed and memory utilization are truly your
overriding goals then use a sorted array of names and a case statement.

registered

Your understanding of virtual vs static methods while accurate is not
complete. With a virtual method it doesn't matter how the method is called,
the method defined for the instance type will be executed. With a static
method the way that the method is called determines the actual method
invoked. The ObjectPascal (Delphi) Language Reference, see the Help table of
contents, is well worth much study. In particular study the Classes and
Objects chapter.

I would suggest that you follow the path of least resistance. Drop the "not
built here" philosophy, and take advantage of work already done by others.

Type
tBase = class (tPersistent)
public
constructor Create; virtual;
end;
tBaseClass = class of tBase;

constructor tBase.Create;

begin
inherited Create;
end;

Descend all of your classes from tBase, or one of its descendants. With a
virtual constructor everything works quite well with a minimum amount of
coding on your part. The code to create a new instance then becomes, simply,

FindClass (aBaseClassDescendantName).Create;
 
 
 

Retry: How to create an instance of a class by class name in a string?

Post by Rob Kenned » Wed, 22 Oct 2003 02:48:58


Not *quite* that simple. You'll need to cast FindClass's return value to
TBaseClass. FindClass returns a TPersistentClass, and without the type
cast, you'll be calling TPersistent.Create, which is not virtual.

--
Rob
 
 
 

Retry: How to create an instance of a class by class name in a string?

Post by Bruce Robe » Wed, 22 Oct 2003 03:00:27


:) Good point.

tBase (FindClass (aBaseClassDescendantName)).Create
 
 
 

Retry: How to create an instance of a class by class name in a string?

Post by Bjge She » Wed, 22 Oct 2003 15:45:42

"Bruce Roberts" < XXXX@XXXXX.COM > skrev i melding




tBaseClass (FindClass (aBaseClassDescendantName)).Create


--
Regards,

Bjge Sher
XXXX@XXXXX.COM
-------------------------------------
I'll not spend any money on American Software products
until armed forces are out of Iraq.
 
 
 

Retry: How to create an instance of a class by class name in a string?

Post by vbdi » Fri, 24 Oct 2003 13:22:30

Im Artikel <Ig5lb.246$ XXXX@XXXXX.COM >, "Bjge Sher"
< XXXX@XXXXX.COM > schreibt:


This could be beautified by a class function:

class function TBaseClass.CreateFromName(const AName: string): TBaseClass;

DoDi
 
 
 

Retry: How to create an instance of a class by class name in a string?

Post by Bruce Robe » Sat, 25 Oct 2003 01:57:39


Oh, very nice.
 
 
 

Retry: How to create an instance of a class by class name in a string?

Post by Marc Youn » Sun, 26 Oct 2003 09:12:54

Thanks to all who have responded. I am going to try all this and ask again
if I get stuck.






TBaseClass;