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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:57:39+00:00 2026-05-12T14:57:39+00:00

I have two pointers to objects and I want to test if they are

  • 0

I have two pointers to objects and I want to test if they are the exact same object in the most robust manner. I explicitly do not want to invoke any operator == overloads and I want it to work no matter what base classes, virtual base classes and multiple inheritance is used.

My current code is this:

((void*)a) == ((void*)b)

And for my case this works. However, that doesn’t work for this case:

class B1 {};
class B2 {};
class C : public B1, public B2 {}

C c;
B1 *a = &c;
B2 *b = &c;

Subbing in reinterpert_cast, static_cast or dynamic_cast doesn’t work either.


Particularly I’m hoping for something that ends up really simple and efficient. Ideally it wouldn’t require any branch instructions to implement and would do something like, adjust the pointer to the start of the object and compare.

  • 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-12T14:57:39+00:00Added an answer on May 12, 2026 at 2:57 pm

    If your classes are genuinely exactly as given then it’s impossible as there’s not enough information available at runtime to reconstruct the required information.

    If they’re actually polymorphic classes, with virtual functions, it sounds like dynamic_cast<void *> is the answer. It returns a pointer to the most derived object. Your check would then be dynamic_cast<void *>(a)==dynamic_cast<void *>(b).

    See paragraph 7 here:

    http://www.csci.csusb.edu/dick/c++std/cd2/expr.html#expr.dynamic.cast

    I suspect the usual dynamic_cast issues apply — i.e., no guarantee it will be quick, and your classes will have to be polymorphic.

    This is not a feature I have used myself, I’m afraid — but I have seen it suggested often enough by people who have that I infer it is widely-supported and works as advertised.

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

Sidebar

Ask A Question

Stats

  • Questions 218k
  • Answers 218k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The regular expression that will match and capture any character… May 12, 2026 at 11:36 pm
  • Editorial Team
    Editorial Team added an answer You can get the TurboPower Abbrevia for 2010 from: http://tpabbrevia.sourceforge.net/ May 12, 2026 at 11:36 pm
  • Editorial Team
    Editorial Team added an answer To get an exact copy of the data, table, and… May 12, 2026 at 11:36 pm

Related Questions

I have a point class like: class Point { public: int x, y; Point(int
I wrote a file parser for a game I'm writing to make it easy
I have a couple of classes that want to pass each other some information
I have a program in which I want to be able to store certain

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.