Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7036729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:27:32+00:00 2026-05-28T01:27:32+00:00

How do I find out the type of a variable when inheritance is involved?

  • 0

How do I find out the type of a variable when inheritance is involved?

I’m having a little situation, I’ll describe it in pseudocode:

class A
{
public:
A();
virtual ~A();

protected:
//some members
};

class B : public A
{
public:
B();
virtual ~B();

protected:
//some members
};

///////////////////////

int main()
{
A* pA = new B();
std::cout<<"type of pA: "<< ???;
}

How can I find out the type of pA? the result should be B. Also, what should I do if I want the result to be A?

Thanks.


EDIT:

I’ll let you be the judge of whether it is bad design or not. If you think it is, then please tell me a better alternative.

Code:

class MyContactReport : public NxUserContactReport
{
    void OnContactNotify(NxContactPair& pair, NxU32 events)
    {
        if (pair.actors[0]->userData == NULL || pair.actors[1]->userData == NULL) return;

        LevelElement* otherObject = (LevelElement*)pair.actors[1]->userData;
        LevelElement* triggerObject = (LevelElement*)pair.actors[0]->userData;

        switch(events)
        {
        case NX_NOTIFY_ON_START_TOUCH:
            triggerObject->OnContactStartTouch(otherObject);
            break;
        case NX_NOTIFY_ON_END_TOUCH :
            triggerObject->OnContactEndTouch(otherObject);
            break;
        case NX_NOTIFY_ON_TOUCH:
            triggerObject->OnContactTouch(otherObject);
            break;
        }
    }
} *myReport;

pair.actors[1]->userData gives me access to the userData from an actor, an actor is something of the PhysX framework, that determines collisions and physics etc. The userdata is of type void*.
This is also the only way to find out object the actor actually belongs to.

Then there’s class LevelElement, an abstract class where every object in my level inherits from (level as in a game-level)

LevelElement has protected virtual methods: OnContactTouch(LevelElement* pOtherElement) etc…
In those methods, I need to find out what type of LevelElement it is, to take certain specific measures.

Is this bad design? If yes, please help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:27:32+00:00Added an answer on May 28, 2026 at 1:27 am

    Use the typeid operator, as described for example here.

    Basically:

    #include <typeinfo>
    

    […]

    std::cout << "typeid(*pA): " << typeid(*pA).name() << std::endl;
    std::cout << "typeid(pA): " << typeid(pA).name() << std::endl;
    

    Results, with g++ 4.4.5:

    typeid(*pA): 1B
    typeid(pA): P1A
    

    I.e. there is some mangling involved at least using gcc. Check out this question for how to deal with this.


    EDIT: As for your design question, instead of checking what type the otherObject is, a better solution is to just tell this object what to do. Assuming you want to code the onContactTouch for a hypothetical Bullet object; instead of

    switch (type) {
    case PLAYER:
        (Player*)otherObject->dealDamage(10);
        break;
    case BULLETPROOF_GLASS:
        (BulletproofGlass*)otherObject->ricochet();
        break;
    }
    

    do this:

    otherObject->onHitByBullet(this);
    

    This is sometimes called the tell, don’t ask principle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP variable of type Array and I would like find out
i want to find out the type of a Variable (the variable is given
How can I find out the type of a NSDictionary element?
Where could I find out the content type for a specific file format/extension? For
how do I find out the return type of a method from the MethodBase?
find . -type f -print prints out ./file1 ./file2 ./file3 Any way to make
Having only a valid GD image resource is it possible to find out the
I got in a session a class with type-safe variable; int, string, dataset, etc
In Java, you can use instanceOf or getClass() on a variable to find out
Update: How to create a out variable?Can anyone please help me find out what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.