passing a type to a function?

  • Thread starter Christof Warlich
  • Start date
C

Christof Warlich

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
 
K

Karl Heinz Buchegger

Christof said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,652
Latest member
Campbellamy

Latest Threads

Top