CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    May 2004
    Posts
    72

    Post Find out type of class

    How do I find out, which type of class the variable is, I catually have.

    Let me give you an example how I mean this:

    Code:
    CRectangle* rectangle =  new CRectangle();
    if (   isClassType(CRectangle, rectangle)    )
    {
              print("the variable rectangle is a CRectangle class");
    }
    The function I search has another name and other variables, but it finds out, what kind of class the variable (or pointer) is.

    I know there is a function like the isClassType() in MFC, but I don`t find it.
    Please help.

  2. #2
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: Find out type of class

    Is typeid what you looking for?

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Find out type of class

    MFC: CObject::IsKindOf

    Also, take a look at GetClassName API

  4. #4
    Join Date
    Oct 2003
    Location
    Romania
    Posts
    127

    Re: Find out type of class

    All-in-one HERE.

  5. #5
    Join Date
    May 2004
    Posts
    72

    Re: Find out type of class

    Quote Originally Posted by VictorN
    MFC: CObject::IsKindOf
    Thanks, that`s it. It`s working fine.

  6. #6
    Join Date
    Aug 2004
    Location
    Earth, Solar System, Milky Way
    Posts
    80

    Re: Find out type of class

    Quote Originally Posted by VictorN
    MFC: CObject::IsKindOf

    Also, take a look at GetClassName API
    What is the connection between CObject::IsKindOf and GetClassName API
    Hokutata Yakubotu

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Find out type of class

    Quote Originally Posted by je-wo
    How do I find out, which type of class the variable is, I catually have.

    Let me give you an example how I mean this:

    Code:
    CRectangle* rectangle =  new CRectangle();
    if (   isClassType(CRectangle, rectangle)    )
    {
              print("the variable rectangle is a CRectangle class");
    }
    Anytime you need to code like this, think about how you've designed the program. More often than not, there is a flaw in your design.

    In C++, you have virtual functions, and the need for doing this is for the most part, unnecessary.

    Regards,

    Paul McKenzie

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Find out type of class

    Quote Originally Posted by Hokutata
    What is the connection between CObject::IsKindOf and GetClassName API
    AFAIK, there is no connection between them.
    They work independently and return different values/value types.

  9. #9
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Find out type of class

    As Paul pointed out, C++ was deliberately designed (originally) to NOT provide run-time type identification. Later versions of the language do support RTTI (it is necessary internally for dynamic_cast). The MFC classes (derived from CObject) have their own methodology. COM classes have a different methodogy.

    Think (and think again) if you really need to make use of any of the available methods. At the very least it will potentially decrease the portability of your code if you use them [actually if you do use MFC or COM you have already reduced the portability of your code ]
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured