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

  • Home
  • SEARCH
  • 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 631343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:57:16+00:00 2026-05-13T19:57:16+00:00

How do I check at runtime if an object is of type ClassA or

  • 0

How do I check at runtime if an object is of type ClassA or of derived type ClassB? In one case I have to handle both instances separately

ClassA* SomeClass::doSomething ( ClassA* )
{
    if( /* parameter is of type base class */) {

    } else if { /* derived class */ ) {

    }
}

Maybe I could say that the derived class ClassB has some special capabilities. But how do I do that without changing the existing class ClassA ?

  • 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-13T19:57:16+00:00Added an answer on May 13, 2026 at 7:57 pm

    It’s generally a very bad idea to switch on the exact type like that. By doing this, you are tightly coupling your method to derived classes of ClassA. You should use polymorphism. Introduce a virtual method in class A, override it in class B and simply call it in your method.

    Even if I was forced to handle the functionality in the external function itself for some reason, I would do something like:

    class ClassA { 
      public: virtual bool hasSpecificFunctionality() { return false; }
    };
    
    class ClassB : public ClassA {
      public: virtual bool hasSpecificFunctionality() { return true; }
    };
    
    ClassA* SomeClass::doSomething ( ClassA* arg )
    {
        if (arg->hasSpecificFunctionality()) {
    
        } else {
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I check the type of a value on runtime? I'd like to
Is it possible at runtime to programmatically check the name of the Thread that
How do you check the windows version in Win32 at runtime?
I have a hierarchy of types - GenericClass and a number of derived classes,
Here is what I want to do: public [type determined at runtime] ImageToShow {
Just want to check: If you have a class that uses a method added
I would like to check whether an object (e.g. someObject ) is assignable (cast-able)
I receive this Run-Time Check Failure upon the return in the following code. I
Can anyone explain to me what this means? Run-Time Check Failure #0 - The
Check out this test: [TestFixture] public class Quick_test { [Test] public void Test() {

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.