Link to home
Start Free TrialLog in
Avatar of ChenChen
ChenChenFlag for Afghanistan

asked on

create an object at run time

hello,
Say I've got a template class called "Cache" that can hold any object. Just simply delare like this :
Cache<ObjType> c.
Now I want to create different Caches for different objects at run time.
Could anyone help?
Avatar of Zoppo
Zoppo
Flag of Germany image

Hi ChenChen,

what exactly do you mean with this? Can you give a
pseudo code example of what you want to do?

ZOPPO
Avatar of nahumd
nahumd

If you just mean a pointer that can hold any object, then you can use "void *" pointer instead.
Avatar of ChenChen

ASKER

Here is an example:

////////////////////////////////////
#include "Cache.h" //where template class "Cache" is defined.

//I can do this
Cache<MyClass1> c1;//it will be a cache holding MyClass1 objects

//or I can also do this
Cache<MyClass2> c2;//it will be a cache holding MYClass2

/*
Now I need to do this at run time rather than compile time. I want the user to be able to choose (from a list) what sort of cache it will be. Creating a full list of  Cache pointers and just allocate the one that has been choosen would be an answer, but I thought it should be a better way.
*/
Are you trying to create an object that can hold different types?

If so, you can do this if the types have either common interface/methods or common inheritance.

Is this the case?
ASKER CERTIFIED SOLUTION
Avatar of andymurd
andymurd

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
yes he's right
Can this article help you?

http://www.cuj.com/articles/2000/0011/0011b/0011b.htm

and other article:
"Concerns in the Design of a Software Cache"