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 7812131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:24:57+00:00 2026-06-02T04:24:57+00:00

For example: class Base{…}; class Sub1 : public Base{…}; class Sub2 : public Base{…};

  • 0

For example:

class Base{...};

class Sub1 : public Base{...};

class Sub2 : public Base{...};

Sub1 s1;
Sub2 s2;

bool is_same_base(void *obj1, void *obj2){
...
}

if(is_same_base(s1,s2){
     printf("Great! S1 and S2 and from same base class!\n");
}

In this case, I am not sure what’s the best/fast way to know if the obj1 and obj2 are derived same base class?

One possible but slowest way is to compare the virtual table entries one by one, if any function pointer exists at both virtual tables, then I can say they are derived from same base class. This is not efficient.

Any comment?

  • 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-06-02T04:24:59+00:00Added an answer on June 2, 2026 at 4:24 am

    You have to know the base class. You can’t use dynamic cast without knowing what type to dynamic cast to. There is also typeid, but again, that does not give you the base class.

    Here’s the way I see it:

    1. In order to use an instance of Sub1 and Sub2, you need to include “Sub1.h” and “Sub2.h” (assuming they’re not declared in the same file).
    2. Sub1.h and Sub2.h need to mention which base class Sub1 and Sub2 inherit from, or the code will not compile.

    In other words, there is no way you can be prevented from simply looking up the common base class yourself, so I don’t see why you would need to do this in the first place.

    Edit: You can’t use void pointers, because then you’re throwing away the type information anyway. The only way you could do this kind of thing is probably at compile time, using Template metaprogramming (I could be wrong), but then your classes need to be written with this kind of use in mind. From what I can tell, it looks like modifying the original class code is not an option.

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

Sidebar

Related Questions

Example: class C { public: void operator =(int i) {} }; class SubC :
if I have for example some class Base and derived from it Derived Also
In the following example: class Base { int x=10; Base() { show(); } void
Look at this example class base { public: int m1; base() { m1 =
For example: I have an implementation (say doSomething()) in Base class. I want all
Why can't I cast a base class instance to a derived class? For example,
All the code examples always use base() as follows class A { public A()
Example: class MyClass { Composition m_Composition; void MyClass() { m_Composition = new Composition( this
I have an example class containing two data points: public enum Sort { First,
Here is code example class A{ int i; public: A(int i) : i(i) {}

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.