?
Solved

create an object at run time

Posted on 2002-05-28
7
Medium Priority
?
207 Views
Last Modified: 2010-04-02
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?
0
Comment
Question by:ChenChen
7 Comments
 
LVL 31

Expert Comment

by:Zoppo
ID: 7041606
Hi ChenChen,

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

ZOPPO
0
 
LVL 2

Expert Comment

by:nahumd
ID: 7041926
If you just mean a pointer that can hold any object, then you can use "void *" pointer instead.
0
 
LVL 4

Author Comment

by:ChenChen
ID: 7043259
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.
*/
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 30

Expert Comment

by:Axter
ID: 7043283
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?
0
 
LVL 1

Accepted Solution

by:
andymurd earned 200 total points
ID: 7044607
You can only instantiate the template at compile time but you can wait until runtime to create objects. The user must have a fixed list of the types of cache available to him/her, then create an object of the right type depending on their choice. You may find it useful to have all the cache types share a common base class.

Here is some (rather poor) example code:

int cache_type = AskUserForCacheType();
CacheBase *pCache = NULL;

switch(cache_type) {
case CACHE_TYPE_1:
    pCache = new Cache<MyClass1>;
    break;

case CACHE_TYPE_2:
    pCache = new Cache<MyClass2>;
    break;

default:
    cerr << "ERROR!";
}

Tag & switch programming as shown in the example is not very OO, so you may wish to consider a class factory.

Hope this helps,
    Andy.



0
 
LVL 3

Expert Comment

by:CoolBreeze
ID: 7047787
yes he's right
0
 

Expert Comment

by:billik
ID: 7494633
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"
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question