create an object at run time

Posted on 2002-05-28
Last Modified: 2010-04-02
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?
Question by:ChenChen
LVL 31

Expert Comment

ID: 7041606
Hi ChenChen,

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


Expert Comment

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

Author Comment

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.
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 30

Expert Comment

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?

Accepted Solution

andymurd earned 50 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>;

case CACHE_TYPE_2:
    pCache = new Cache<MyClass2>;

    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,


Expert Comment

ID: 7047787
yes he's right

Expert Comment

ID: 7494633
Can this article help you?

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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

831 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