# Function Pointer

Hi,

I have a piece of code below:

//base class Employee, derive class manager, engineer, clerk
vector<Employee*>  employee_vec;

if ( strcmp( input, "manager") == 0) {
employee_vec.push_back(new manager());
}
else if ( strcmp( input, "engineer") == 0) {
employee_vec.push_back(new enginner());
}
else if ( strcmp( input, "clerk") == 0) {
employee_vec.push_back(new clerk());
}

(employee_vec[employee_vec.size() -1]) -> setSalary();
(employee_vec[employee_vec.size() -1]) -> setJobcode();

1. I'm looking that is there any better method/new technique  that just use one/two line to replace the if/else loop?
2. Or this if/else loop is the simple yet most efficient way to do it?
3. Basically, would like to learn any new techique or more efficient way same purpose

thanks.
###### Who is Participating?

Commented:
the above code is probably the best way to do what your doing. There is no need to using function pointers.
You seem to have used OOP techniques well, in that, your using polymorphism.
0

Author Commented:
will function pointer is one of solution?
0

Author Commented:
why function pointers is no needed in this place? Is function pointer use for other purpose?

thanks
0

Commented:
In your example it is not needed to use a vector. Just a normal Employee* would do.
But I agree with Jase-Coder that there is no need to use function pointers. Function pointers would be more suitable if your functions do not belong to a class.

I do not know a situation in C++ which requires a function pointer. But if you want to create a function in C like for_each in this example: http://www.research.att.com/~bs/bs_faq2.html#function-object, you need a function pointer. C++ has simply OOP techniques to do the same.
0

Commented:
functions allow you to pass the address of functions to another function. For example, I created a schedule class, which accepted a function pointer as an argument. When the schedule time was met, my class would call the function passed to the class. So, by using function pointers I allowed the user of my class to control the functionality of the schedule task.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.