Solved

create an object at run time

Posted on 2002-05-28
7
197 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 30

Expert Comment

by:Zoppo
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 30

Expert Comment

by:Axter
Comment Utility
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 50 total points
Comment Utility
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
Comment Utility
yes he's right
0
 

Expert Comment

by:billik
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now