Function Pointer

Posted on 2006-05-16
Last Modified: 2013-11-17

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

Question by:bhuey_ling

    Author Comment

    will function pointer is one of solution?  
    LVL 11

    Accepted Solution

    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.

    Author Comment

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

    LVL 14

    Assisted Solution

    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:, you need a function pointer. C++ has simply OOP techniques to do the same.
    LVL 11

    Assisted Solution

    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.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project ( automates most of the tasks discussed in this article. You can even fin…
    Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
    The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
    The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now