By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 2,075 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

passing a type to a function?

P: n/a
Hi,

I'm looking for an elegant way to do the following:

The classes DA1, ..., DAn are all derived from class A. The function
A *f(?????);
shall return a generalized pointer to an object of one of the derived
classes,
depending on the (to be defined) parameter being passed. If the requested
derived classes' object does not yet exist, it shall be created by the
function.

I certainly can do this by passing an enum to function f that enumerates
all possible derivations of class A, and instantiating or retrieving the
requested
object in a switch statement.

Any suggestions on how this could be done better?

Thanks and regards,

Christof
Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Christof Warlich wrote:

Hi,

I'm looking for an elegant way to do the following:

The classes DA1, ..., DAn are all derived from class A. The function
A *f(?????);
shall return a generalized pointer to an object of one of the derived
classes,
depending on the (to be defined) parameter being passed. If the requested
derived classes' object does not yet exist, it shall be created by the
function.

I certainly can do this by passing an enum to function f that enumerates
all possible derivations of class A, and instantiating or retrieving the
requested
object in a switch statement.

Any suggestions on how this could be done better?


What you are looking for is called an 'object factory'.
Search the web for that and you will find some different
ways to implement one.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.