Learn how to a build a cloud-first strategyRegister Now

x
Solved

Function Pointer

Posted on 2006-05-16
Medium Priority
359 Views
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.
0
Question by:bhuey_ling
• 2
• 2

Author Comment

ID: 16696583
will function pointer is one of solution?
0

LVL 11

Accepted Solution

Jase-Coder earned 300 total points
ID: 16696789
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 Comment

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

thanks
0

LVL 14

Assisted Solution

cwwkie earned 150 total points
ID: 16701717
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

LVL 11

Assisted Solution

Jase-Coder earned 300 total points
ID: 16706193
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

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even finâ€¦
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
Suggested Courses
Course of the Month20 days, 17 hours left to enroll