If I have two objects (Intern and Manager) derived from an Employee class, could I create a function that would return a pointer to a new instance of one of the two derived objects by passing the type to the function. So, could I do something like this (I know this gives me an error, the example is just for clarity of what I'm trying to do):

 Employee* createEmployee(typename T){
        return ( new T() ) ;
 }

So, if I wanted to create a new Manager, I would say createEmployee(Manager), and I would get a pointer to a new Manager object.

could I do something like this

Yes. The mechanism to do this is called "templates". Your function would look like this:

template <typename T>
T* createEmployee() {
  return new T();
};

And it would be called as so:

Employee* p = createEmployee< Manager >();

For a more complete tutorial on this, refer to this, or this, or any good book on C++.

Also, I must remark on the fact that you should not return a new-allocated object by pointer from a function. This is bad style because it obscurs the method of allocation, and thus, creates confusion as to the method of destruction (with delete in this case). One option is to always provide a pair of functions, one for creation one for destruction:

template <typename T>
T* createEmployee() {
  return new T();
};

template <typename T>
void destroyEmployee(T* p) {
  delete p;
};

And make it clear in the documentation that anything created with the one function must be destroyed with its corresponding function.

Another option is to use a smart-pointer to (implicitly) bundle the pointer with a deallocation method. One such smart-pointer is std::shared_ptr (from <memory>):

template <typename T>
std::shared_ptr<T> createEmployee() {
  return std::shared_ptr<T>(new T());
};

This smart-pointer is reference-counted and will automatically destroy the object when there are no more shared_ptr to that object in existence (i.e., the object is no longer needed). In other words, you don't need to explicitly delete that pointer, it will delete itself when appropriate.

In fact, the standard library includes a very similar function template, called std::make_shared, which allows you to do this:

std::shared_ptr<Employee> p = std::make_shared< Manager >();

And, even more convenient, if you need to pass some parameters to the creation of that Manager object, you can also pass them to make_shared and they will be related to the creation of the Manager object, e.g., if you have a name and employee number to give upon construction, you can do this:

std::shared_ptr<Employee> p = std::make_shared<Manager>("John Smith", 42);

which is equivalent to:

std::shared_ptr<Employee> p( new Manager("John Smith", 42) );

This requires basic C++11 support (latest standard from 2011). Otherwise, you can use std::tr1::shared_ptr, or boost::shared_ptr (from Boost libraries).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.