473,406 Members | 2,220 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Passing an evaluated variable to new in allocating a derived class

Hi All,

I am trying to figure out any way to pass an evaluated variable to new?
Please let me know if it is possible.

Please see the example structure of my code as below

class baseclass
{
private:
int foo;
int bar;
public:
virtual void run_func(void)=0;
};

class derivclass_1:public baseclass
{
private:
int xvz;
public:
void run_func(void) { /* do something interesting */}
};

I have the derived classes like derivclass_1,derivclass_2,derivclass_3
etc till derivclass_30

my present way of calling goes something like this

baseclass *bc;
if(x==1)
bc=new derivclass_1;
if(x==2)
bc=new derivclass_2;
if(x==3)
bc=new derivclass_3;

.....
......

if(x==30)
bc=new derivclass_30;
which is looking very inefficient to me.

I want to do SOMETHING like

bc=new derivclass_x;

which will save me alot of lines of code as well as helping me in
isolating the main code if I want to add derivclass_31 at a later point
of time.

Is it possible to do (somehow)?

Thanks
Raghu

Jan 17 '06 #1
7 1394
* Raghu Kodali:

my present way of calling goes something like this

baseclass *bc;
if(x==1)
bc=new derivclass_1;
if(x==2)
bc=new derivclass_2;
if(x==3)
bc=new derivclass_3;

....
.....

if(x==30)
bc=new derivclass_30;
which is looking very inefficient to me.

I want to do SOMETHING like

bc=new derivclass_x;

which will save me alot of lines of code as well as helping me in
isolating the main code if I want to add derivclass_31 at a later point
of time.

Is it possible to do (somehow)?


Map id's to factory functions, e.g. an array or std::map or more
elaborate factory repository, depending on your requirements.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jan 17 '06 #2
Alf P. Steinbach wrote:
* Raghu Kodali:
my present way of calling goes something like this

baseclass *bc;
if(x==1)
bc=new derivclass_1;
if(x==2)
bc=new derivclass_2;
if(x==3)
bc=new derivclass_3;

....
.....

if(x==30)
bc=new derivclass_30;
which is looking very inefficient to me.

I want to do SOMETHING like

bc=new derivclass_x;

which will save me alot of lines of code as well as helping me in
isolating the main code if I want to add derivclass_31 at a later point
of time.

Is it possible to do (somehow)?

Map id's to factory functions, e.g. an array or std::map or more
elaborate factory repository, depending on your requirements.


Initialising that map is going to have pretty much the same number of
lines. But now there will be two places to worry about: the map
initialisation code *and* each class implementation where you need to
add a "factory function". I am not really sure how it's superior to
this, which is essentially a switch statement. If the map is not
an 'std::map' but an array of pointers to functions, it's probably
marginally faster than a switch statement. The added complexity of
the classes is what would kill it for me, though.

V
Jan 17 '06 #3
On 17 Jan 2006 08:21:17 -0800, "Raghu Kodali" <us**********@gmail.com>
wrote:
Hi All,

I am trying to figure out any way to pass an evaluated variable to new?
Please let me know if it is possible.

Please see the example structure of my code as below

class baseclass
{
private:
int foo;
int bar;
public:
virtual void run_func(void)=0;
};

class derivclass_1:public baseclass
{
private:
int xvz;
public:
void run_func(void) { /* do something interesting */}
};

I have the derived classes like derivclass_1,derivclass_2,derivclass_3
etc till derivclass_30

my present way of calling goes something like this

baseclass *bc;
if(x==1)
bc=new derivclass_1;
if(x==2)
bc=new derivclass_2;
if(x==3)
bc=new derivclass_3;

....
.....

if(x==30)
bc=new derivclass_30;
which is looking very inefficient to me.

I want to do SOMETHING like

bc=new derivclass_x;

which will save me alot of lines of code as well as helping me in
isolating the main code if I want to add derivclass_31 at a later point
of time.

Is it possible to do (somehow)?


Can x be known at compile time? If so, you can use template classes
which can all derive from the non-template base class. Otherwise,
you're pretty much stuck with the above, or you could wrap your giant
switch statement in some kind of factory function.

--
Bob Hairgrove
No**********@Home.com
Jan 17 '06 #4
There is no way I am going to know x at compile time. x is basically
derived from one of the command line arguments passed on to the
program.

So, I think I am stuck with my if/switch-case.

Thanks
Raghu

Jan 18 '06 #5
* Raghu Kodali:
There is no way I am going to know x at compile time. x is basically
derived from one of the command line arguments passed on to the
program.

So, I think I am stuck with my if/switch-case.


Did you read my earlier reply?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jan 18 '06 #6

Alf P. Steinbach wrote:
* Raghu Kodali:
There is no way I am going to know x at compile time. x is basically
derived from one of the command line arguments passed on to the
program.

So, I think I am stuck with my if/switch-case.


Did you read my earlier reply?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Hi Alf,

I did read your message. I am not very comfortable with factory
functions & map. I started reading them yesterday after your mail. I
will update you pretty soon wether it works for me or not.

Thanks
Raghu

Jan 18 '06 #7
TB
Raghu Kodali sade:
Alf P. Steinbach wrote:
* Raghu Kodali:
There is no way I am going to know x at compile time. x is basically
derived from one of the command line arguments passed on to the
program.

So, I think I am stuck with my if/switch-case.

Did you read my earlier reply?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Hi Alf,

I did read your message. I am not very comfortable with factory
functions & map. I started reading them yesterday after your mail. I
will update you pretty soon wether it works for me or not.

Thanks
Raghu


Here's one solution if you feel that your current if-statements are
ugly/insufficient/or something.

#include <new>
#include <vector>

class BaseClass {};
class DerivedClass_1 : public BaseClass {};
class DerivedClass_2 : public DerivedClass_1 {};

template<typename T>
class Allocator {
public:
static BaseClass * Alloc() throw (std::bad_alloc) {
return new T;
};
};

std::vector<BaseClass * (*)()> AllocateIndexClass;

#define POPULATE(VECTOR,CLASS) \
VECTOR.push_back(&Allocator<CLASS>::Alloc)

int main(int argc, char* argv[])
{
POPULATE(AllocateIndexClass,BaseClass);
POPULATE(AllocateIndexClass,DerivedClass_1);
POPULATE(AllocateIndexClass,DerivedClass_2);

// allocate a new DerivedClass_2 object
BaseClass * bc = AllocateIndexClass[2]();
delete bc;

return 0;
}

Or if you need more control of the associated index use this
macro with a std::map<unsigned,BaseClass * (*)()> instead:

#define POPULATE(MAP,INDEX,CLASS) \
MAP[INDEX] = &Allocator<CLASS>::Alloc

This way provides more control over what class a certain index
does allocate. You could change it during execution to
adjust it to the situation.

Of course, you could use templates instead of macros.

--
TB @ SWEDEN
Jan 18 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
20
by: joe | last post by:
Hi all! I just have quick, possibly stupid question.... is it possible to do the following: int func(){ int *pointer; foo(pointer); } int foo(int *pointer){
6
by: MSDNAndi | last post by:
Hi, I have a baseclass (non-static) with some static and some non-static methods/fields/properties. In the baseclass in one of the static methods I need to do something like " somelogic...
4
by: Panos Laganakos | last post by:
Hello, I'd like to know how its possible to pass a data attribute as a method parameter. Something in the form of: class MyClass: def __init__(self): self.a = 10
4
by: ingoweiss | last post by:
Hi, I am having trouble passing parameters of a Javascript subclass constructor through to it's superclass constructor. I am trying all sorts of things, including the below, but nothing...
2
by: reckless2k | last post by:
Client side; knows nothing of Derived: class Base { ... virtual void do_something() } #include "Base.h" void main() {
3
by: Bit Byte | last post by:
Must be the time of the day, but I seem to get my head in a spin over this ... I have a base class B, from which I have a derived class. In class B, i have a (public access) typedef of a...
1
by: autumn | last post by:
Hi everybody, I'm having problem passing pointer to member object as template argument, seems VC 2005 does not allow 'pointer to base member' to 'pointer to derived member' conversion in template...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.