Thread: Debug function using typeid?

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    2

    Debug function using typeid?

    Hi everyone,

    I'm trying to make a generic logger class for help with debugging an application I'm working on (project that I can't use any debuggers with for a plethora of reasons).

    Anyway, I'm trying to create a simple static class that I can call methods on from within any polymorphic object and have it automatically output the name of the object's class.

    This is what I have so far, and it doesn't actually work haha

    Code:
    string Logger::debugMessage(void *o, string outText)
    {
    	string ret = typeid(o).name()+string(": ")+outText+string(" ");
    	Logger::OutputText(const_cast<char *>(ret.c_str()));
    	return ret;
    }
    The idea is that I call it as such (from within any class - everything I want to test on has virtual methods so typeid shouldnt be a problem I think):

    Code:
    void someClass::someMethod()
    {
    	macros::debugMessage(this, "calling someMethod");
    }
    Going through everything and adding a 'typeName' property isn't really an option as far as I see it, I'd much rather do this properly than resort to a hacky method like that.

    Thanks for any help
    cheers

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    And what does "doesn't work" mean? I lost my crystal ball.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    2
    haha sorry.

    Well I get no output at all from typeid(o).name(), so my assumption is that typeid is failing silently (or maybe not so silently, theres no error catching here). What I'm wondering is if using typeid on a void pointer is ok, if passing 'this' into the function will work, and if a combination of the two might result in it not returning anything.

    cheers

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It's ok - but it won't give you what you want. At best, it'll return the type info for the type "void*". At worst, it returns a dummy type info.

    There is NO way of obtaining the type of a class at runtime from a void pointer. You need at least a pointer to a base class.

    Use a template instead, it ought to work better.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  5. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM