If I have a class named Employee, what should I do in order to determine that an object is really of type Employee? Thank you in advance. Any criticisms about my problem being described as too vague is accepted haha
If I have a class named Employee, what should I do in order to determine that an object is really of type Employee? Thank you in advance. Any criticisms about my problem being described as too vague is accepted haha
Read up on RTTI, the Runtime Type Identification.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
You could use dynamic_cast or typeid provided your Employee has a virtual member.
i.e.
What a coincidence? Having a similar discussion here:Code:void do_something(void* ptr) { Employee* e = dynamic_cast<Employee*>(ptr); if (e != 0) { // ptr points to an Employee object } }
http://cboard.cprogramming.com/showthread.php?t=54985
I think what your asking about is inheritance. You need to determine what type of relationship it has to the base class. i.e. "is a" or "has a"
for example: mechanic is a employee. And would inherit members of employee such at hire date and so on from the base class Employee.
On the other hand you wouldnt want to say that mechanic is a hire date. It would be better to inherit that from a base class so that you could say mechanic has a hire date.
In other words you wouldnt want to create a class called hire date. Instead let that be a member of class employee. If it is something that the derived classe's could use make it a member of the base class. If you need more specific functionality make it a derived class such as mechanic.
big146
Look into RTTI, as CornedBee said. Beware when using RTTI, though, because it is generally fairly expensive in terms of performance. If you can get around having to specifically identify the type of an object without jumping through too many hoops, it is generally preferable.