Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

create an object at run time

Posted on 2002-05-28
7
Medium Priority
?
206 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

636 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