?
Solved

create an object at run time

Posted on 2002-05-28
7
Medium Priority
?
204 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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…
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 learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

770 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