ChenChen
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?
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?
If you just mean a pointer that can hold any object, then you can use "void *" pointer instead.
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.
*/
//////////////////////////
#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?
If so, you can do this if the types have either common interface/methods or common inheritance.
Is this the case?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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"
http://www.cuj.com/articles/2000/0011/0011b/0011b.htm
and other article:
"Concerns in the Design of a Software Cache"
what exactly do you mean with this? Can you give a
pseudo code example of what you want to do?
ZOPPO