Fatemeh Karimi Fatemeh Karimi -4 years ago 87
C++ Question

how to figure out which type is passed to a template-defined function?

I have a problem doing my homework. the problem I faced is this:
the question is this:
((write a program that prints the name of the human/employee with maximum sort_param.use template programming.))
human and employee are defined like this:

struct human
{
char name[30];
char * family;
int id;
int sort_param;
};

struct employee
{
human h;
char post[50];
int sort_param;
};


as you see, printing the name of the employee/human depends on the type that is passed to the function.
my question is:
how can I tell the computer to act depend on the data type. I mean if the type is employee then:

cout << employee.h.name << endl;


if the type is human then:

cout << human.name << endl;

Answer Source

use an overloaded function, just call your function with one of arguments those type can be human or employee.

void print_name(const employee& emp)
{
     cout << emp.h.name << endl;
}
void print_name(const human& hum)
{
     cout << hum.name << endl;
}

The argument are edited.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download