Thread: Determining a user-defined class type

  1. #1
    Registered User
    Join Date
    Jun 2003

    Determining a user-defined class type

    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

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Read up on RTTI, the Runtime Type Identification.
    All the buzzt!

    "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

  3. #3
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    You could use dynamic_cast or typeid provided your Employee has a virtual member.


    void do_something(void* ptr)
      Employee* e = dynamic_cast<Employee*>(ptr);
      if (e != 0)
        // ptr points to an Employee object
    What a coincidence? Having a similar discussion here:
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

  4. #4
    Registered User big146's Avatar
    Join Date
    Apr 2003
    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.

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM