pointer to class(not object)

Started by
5 comments, last by Zoomby 20 years, 11 months ago
hi, don''t know if it''s really right, but I somewhere saw a function which got a pointer to a class as parameter, so an object of this specific class was created inside the function. would work like this: class C { }; void* classPtr=&C *classPtr obj=new *classPtr; is that possible, and if not, how could one achieve it? bye chris
Advertisement
quote:Original post by Zoomby
don''t know if it''s really right, but I somewhere saw a function which got a pointer to a class as parameter, so an object of this specific class was created inside the function.

You''re talking about C++ right? Please specify the language when asking questions in a General Programming language forum. The default language around here is Lisp.
quote:
is that possible, and if not, how could one achieve it?

Nope, not in a Standard way. You''d simulate it, probably using some sort of Factory or Virtual Constructor idiom, or maybe using RTTI. Anyway, what''s your real question?
you can do stuff a bit like it with templates... but from what i know, the answer is no. you can however do it in UnrealScript, but i doubt that would help you a lot here :/
thanks for the reply and sorry for not saying it is c++. :-)
I don't have a use for it right now, just wondered if it is possible.

bye
chris

[edited by - Zoomby on May 8, 2003 7:51:04 AM]
Hmmm in the Half-Life SDK i have read something about pointers to classes aswell but i couldn''t figure out how it worked because it was wrapped in #defines. But i know it was used to register the game object classes with the game. Like
LINK_ENTITY_TO_CLASS(monster, CMonster); // <-- monster without quotes!

And in WorldCraft you could set a CMonster by putting a monster-Entity.

I would like to know how to do that, aswell...
I don''t know whether you''re talking about the MFC idiom, as in

CRuntimeClass *pCls = RUNTIME_CLASS(CSomeClass);
CSomeClass *pObj = pCls->CreateObject();

That only works in MFC, and relies upon macros being placed in every class that you want to create this way. If you''re not using MFC, you should be able to roll your own implementation of this if you want to.
The HL SDK is full of a bunch of glorious hacks, you''d probably want to try and stay away from emulating a lot of its behavior The LINK_ENTITY_TO_CLASS macro turns the first parameter into the name of an actual function, and the second parameter into the object type to create using some allocating and initialization code, with the second parameter as the type. When the engine encounters a given entity, it knows that the name of the entity is the exact name of the function in which it can call to create that entity, because of said macro. That''s how it works, yet it''s very hacky.

This topic is closed to new replies.

Advertisement